Created
April 18, 2017 15:49
-
-
Save cenkbircanoglu/519fd804dffc31febd3ae5a07c459de3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"from scipy import sparse, io\n", | |
"import os\n", | |
"import pickle\n", | |
"\n", | |
"from scipy.sparse import csr_matrix\n", | |
"\n", | |
"OUTPUT_PATH = \"data/\"\n", | |
"ngram_list = []\n", | |
"\n", | |
"\n", | |
"class DataTypes:\n", | |
" TRAIN = \"train\"\n", | |
" TEST = \"test\"\n", | |
"\n", | |
"\n", | |
"def create_output_filename(data_type, n=1, name=\"query\"):\n", | |
" return os.path.join(OUTPUT_PATH, \"%s.%s.%s.pickle\" % (name, data_type, n))\n", | |
"\n", | |
"\n", | |
"def ngrams(text, n):\n", | |
" for seq in split_by_n(text, n):\n", | |
" if not seq in ngram_list:\n", | |
" ngram_list.append(seq)\n", | |
"\n", | |
"\n", | |
"remove_newline = lambda x: x.replace(\"\\n\", \"\")\n", | |
"\n", | |
"create_ngram_list = lambda x: ngrams(\n", | |
" '#%s#' % ''.join(ch.lower() for ch in x if ch.isalnum()), 3)\n", | |
"\n", | |
"\n", | |
"def split_by_n(seq, n):\n", | |
" \"\"\"A generator to divide a sequence into chunks of n units.\"\"\"\n", | |
" while seq:\n", | |
" yield seq[:n]\n", | |
" seq = seq[n:]\n", | |
"\n", | |
"\n", | |
"def load_data(path):\n", | |
" global ngram_list\n", | |
" with open(path, mode=\"rb\") as f:\n", | |
" queries_titles = np.array([line.split(\"\\t\") for line in f.readlines()])\n", | |
"\n", | |
" v_remove_newline = np.vectorize(remove_newline)\n", | |
" queries, titles = v_remove_newline(queries_titles[:, 0]), v_remove_newline(queries_titles[:, 1])\n", | |
" v_create_ngram_list = np.vectorize(create_ngram_list)\n", | |
" v_create_ngram_list(queries)\n", | |
" v_create_ngram_list(titles)\n", | |
" ngram_list = np.array(ngram_list)\n", | |
" return queries, titles\n", | |
"\n", | |
"\n", | |
"def convert2matrix(values):\n", | |
" mat = np.zeros((values.shape[0], ngram_list.shape[0]))\n", | |
" for i, value in enumerate(values):\n", | |
" aa = list(split_by_n('#%s#' % ''.join(ch.lower() for ch in value if ch.isalnum()), 3))\n", | |
" mat[i] = np.in1d(ngram_list, aa)\n", | |
" return mat\n", | |
"\n", | |
"\n", | |
"def main(path, data_type, n=1):\n", | |
" queries, titles = load_data(path)\n", | |
" queries_matrix = sparse.csr_matrix(convert2matrix(queries))\n", | |
" titles_matrix = sparse.csr_matrix(convert2matrix(titles))\n", | |
" write2disk(queries_matrix, data_type, n=n, name=\"query\")\n", | |
" write2disk(titles_matrix, data_type, n=n, name=\"doc\")\n", | |
" write2disk(ngram_list, data_type, n=n, name=\"ngram\")\n", | |
" return queries_matrix, titles_matrix, ngram_list\n", | |
"\n", | |
"\n", | |
"def write2disk(data, data_type, n=1, name=None):\n", | |
" output = create_output_filename(data_type, n=n, name=name)\n", | |
" try:\n", | |
" data.astype(np.int64)\n", | |
" np.savez(output, data=data.data, indices=data.indices,\n", | |
" indptr=data.indptr, shape=data.shape)\n", | |
" except Exception as e:\n", | |
" with open(output, mode=\"wb\") as f:\n", | |
" pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)\n", | |
" return output\n", | |
"\n", | |
"\n", | |
"def load_sparse_csr(filename):\n", | |
" # here we need to add .npz extension manually\n", | |
" loader = np.load(filename + '.npz')\n", | |
" return csr_matrix((loader['data'], loader['indices'], loader['indptr']),\n", | |
" shape=loader['shape'])\n", | |
"\n", | |
"\n", | |
"def read_ngramlist(output):\n", | |
" return pickle.load(open(output, mode=\"rb\"))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import pickle\n", | |
"import random\n", | |
"import time\n", | |
"import sys\n", | |
"import numpy as np\n", | |
"import tensorflow as tf\n", | |
"\n", | |
"flags = tf.app.flags\n", | |
"FLAGS = flags.FLAGS\n", | |
"\n", | |
"flags.DEFINE_string('summaries_dir', 'dssm-400-120-relu', 'Summaries directory')\n", | |
"flags.DEFINE_float('learning_rate', 0.1, 'Initial learning rate.')\n", | |
"flags.DEFINE_integer('max_steps', 900000, 'Number of steps to run trainer.')\n", | |
"flags.DEFINE_integer('epoch_steps', 18000, \"Number of steps in one epoch.\")\n", | |
"flags.DEFINE_integer('pack_size', 2000, \"Number of batches in one pickle pack.\")\n", | |
"flags.DEFINE_bool('gpu', 0, \"Enable GPU or not\")\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Loading data from HDD to memory: 0.01s\n", | |
"Loss Function\n", | |
"Step 0\n" | |
] | |
}, | |
{ | |
"ename": "AttributeError", | |
"evalue": "'NoneType' object has no attribute 'shape'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-8-9721edf564cc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 193\u001b[0m \u001b[1;31m# #print(t2-t1)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 194\u001b[0m \u001b[1;31m# t1 = time.time()\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 195\u001b[1;33m \u001b[0msess\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtrain_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfeed_dict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch_idx\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mFLAGS\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpack_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 196\u001b[0m \u001b[1;31m# t2 = time.time()\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 197\u001b[0m \u001b[1;31m# fbp_time += t2 - t1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m<ipython-input-8-9721edf564cc>\u001b[0m in \u001b[0;36mfeed_dict\u001b[1;34m(Train, batch_idx)\u001b[0m\n\u001b[0;32m 151\u001b[0m \u001b[1;34m\"\"\"Make a TensorFlow feed_dict: maps data onto Tensor placeholders.\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 152\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mTrain\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 153\u001b[1;33m \u001b[0mquery_in\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdoc_in\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpull_batch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mquery_train_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdoc_train_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch_idx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 154\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 155\u001b[0m \u001b[0mquery_in\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdoc_in\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpull_batch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mquery_test_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdoc_test_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch_idx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m<ipython-input-8-9721edf564cc>\u001b[0m in \u001b[0;36mpull_batch\u001b[1;34m(query_data, doc_data, batch_idx)\u001b[0m\n\u001b[0;32m 129\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mpull_batch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mquery_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdoc_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatch_idx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 130\u001b[0m \u001b[0mstart\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 131\u001b[1;33m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch_idx\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mBS\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch_idx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mBS\u001b[0m \u001b[1;33m,\u001b[0m\u001b[0mquery_data\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 132\u001b[0m \u001b[0mquery_in\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mquery_data\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mbatch_idx\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mBS\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch_idx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mBS\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 133\u001b[0m \u001b[0mdoc_in\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdoc_data\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mbatch_idx\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mBS\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatch_idx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mBS\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'shape'" | |
] | |
} | |
], | |
"source": [ | |
"start = time.time()\n", | |
"\n", | |
"doc_train_data = None\n", | |
"query_train_data = None\n", | |
"\n", | |
"# load test data for now\n", | |
"query_test_data = load_sparse_csr(create_output_filename(DataTypes.TEST, n=1, name=\"query\"))\n", | |
"doc_test_data = load_sparse_csr(create_output_filename(DataTypes.TEST, n=1, name=\"doc\"))\n", | |
"\n", | |
"\n", | |
"def load_train_data(pack_idx):\n", | |
" global doc_train_data, query_train_data\n", | |
" doc_train_data = None\n", | |
" query_train_data = None\n", | |
" start = time.time()\n", | |
" doc_train_data = load_sparse_csr(create_output_filename(DataTypes.TRAIN, n=int(pack_idx), name=\"doc\"))\n", | |
" query_train_data = load_sparse_csr(create_output_filename(DataTypes.TRAIN, n=int(pack_idx), name=\"query\"))\n", | |
" end = time.time()\n", | |
" print (\"\\nTrain data %d/9 is loaded in %.2fs\" % (pack_idx, end - start))\n", | |
"\n", | |
"\n", | |
"end = time.time()\n", | |
"print(\"Loading data from HDD to memory: %.2fs\" % (end - start))\n", | |
"ngrams = read_ngramlist(create_output_filename(DataTypes.TRAIN, n=1, name=\"ngram\"))\n", | |
"TRIGRAM_D = ngrams.shape[0]\n", | |
"\n", | |
"NEG = 50\n", | |
"BS = 100 # BS = 1000\n", | |
"\n", | |
"L1_N = 400\n", | |
"L2_N = 120\n", | |
"\n", | |
"query_in_shape = np.array([BS, TRIGRAM_D], np.int64)\n", | |
"doc_in_shape = np.array([BS, TRIGRAM_D], np.int64)\n", | |
"\n", | |
"\n", | |
"def variable_summaries(var, name):\n", | |
" \"\"\"Attach a lot of summaries to a Tensor.\"\"\"\n", | |
" with tf.name_scope('summaries'):\n", | |
" mean = tf.reduce_mean(var)\n", | |
" tf.scalar_summary('mean/' + name, mean)\n", | |
" with tf.name_scope('stddev'):\n", | |
" stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean)))\n", | |
" tf.scalar_summary('sttdev/' + name, stddev)\n", | |
" tf.scalar_summary('max/' + name, tf.reduce_max(var))\n", | |
" tf.scalar_summary('min/' + name, tf.reduce_min(var))\n", | |
" tf.histogram_summary(name, var)\n", | |
"\n", | |
"\n", | |
"with tf.name_scope('input'):\n", | |
" # Shape [BS, TRIGRAM_D].\n", | |
" query_batch = tf.sparse_placeholder(tf.float32, name='QueryBatch')\n", | |
" # Shape [BS, TRIGRAM_D]\n", | |
" doc_batch = tf.sparse_placeholder(tf.float32, name='DocBatch')\n", | |
"\n", | |
"with tf.name_scope('L1'):\n", | |
" l1_par_range = np.sqrt(6.0 / (TRIGRAM_D + L1_N))\n", | |
" weight1 = tf.Variable(tf.random_uniform([TRIGRAM_D, L1_N], -l1_par_range, l1_par_range))\n", | |
" bias1 = tf.Variable(tf.random_uniform([L1_N], -l1_par_range, l1_par_range))\n", | |
" variable_summaries(weight1, 'L1_weights')\n", | |
" variable_summaries(bias1, 'L1_biases')\n", | |
"\n", | |
" # query_l1 = tf.matmul(tf.to_float(query_batch),weight1)+bias1\n", | |
" query_l1 = tf.sparse_tensor_dense_matmul(query_batch, weight1) + bias1\n", | |
" # doc_l1 = tf.matmul(tf.to_float(doc_batch),weight1)+bias1\n", | |
" doc_l1 = tf.sparse_tensor_dense_matmul(doc_batch, weight1) + bias1\n", | |
"\n", | |
" query_l1_out = tf.nn.relu(query_l1)\n", | |
" doc_l1_out = tf.nn.relu(doc_l1)\n", | |
"\n", | |
"with tf.name_scope('L2'):\n", | |
" l2_par_range = np.sqrt(6.0 / (L1_N + L2_N))\n", | |
"\n", | |
" weight2 = tf.Variable(tf.random_uniform([L1_N, L2_N], -l2_par_range, l2_par_range))\n", | |
" bias2 = tf.Variable(tf.random_uniform([L2_N], -l2_par_range, l2_par_range))\n", | |
" variable_summaries(weight2, 'L2_weights')\n", | |
" variable_summaries(bias2, 'L2_biases')\n", | |
"\n", | |
" query_l2 = tf.matmul(query_l1_out, weight2) + bias2\n", | |
" doc_l2 = tf.matmul(doc_l1_out, weight2) + bias2\n", | |
" query_y = tf.nn.relu(query_l2)\n", | |
" doc_y = tf.nn.relu(doc_l2)\n", | |
"\n", | |
"with tf.name_scope('FD_rotate'):\n", | |
" # Rotate FD+ to produce 50 FD-\n", | |
" temp = tf.tile(doc_y, [1, 1])\n", | |
"\n", | |
" for i in range(NEG):\n", | |
" rand = int((random.random() + i) * BS / NEG)\n", | |
" doc_y = tf.concat(0, [doc_y,\n", | |
" tf.slice(temp, [rand, 0], [BS - rand, -1]),\n", | |
" tf.slice(temp, [0, 0], [rand, -1])])\n", | |
"\n", | |
"with tf.name_scope('Cosine_Similarity'):\n", | |
" # Cosine similarity\n", | |
" query_norm = tf.tile(tf.sqrt(tf.reduce_sum(tf.square(query_y), 1, True)), [NEG + 1, 1])\n", | |
" doc_norm = tf.sqrt(tf.reduce_sum(tf.square(doc_y), 1, True))\n", | |
"\n", | |
" prod = tf.reduce_sum(tf.mul(tf.tile(query_y, [NEG + 1, 1]), doc_y), 1, True)\n", | |
" norm_prod = tf.mul(query_norm, doc_norm)\n", | |
"\n", | |
" cos_sim_raw = tf.truediv(prod, norm_prod)\n", | |
" cos_sim = tf.transpose(tf.reshape(tf.transpose(cos_sim_raw), [NEG + 1, BS])) * 20\n", | |
"\n", | |
"with tf.name_scope('Loss'):\n", | |
" # Train Loss\n", | |
" print(\"Loss Function\")\n", | |
" prob = tf.nn.softmax((cos_sim))\n", | |
" hit_prob = tf.slice(prob, [0, 0], [-1, 1])\n", | |
" loss = -tf.reduce_sum(tf.log(hit_prob)) / BS\n", | |
" tf.scalar_summary('loss', loss)\n", | |
"\n", | |
"with tf.name_scope('Training'):\n", | |
" # Optimizer\n", | |
" train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(loss)\n", | |
"\n", | |
"# with tf.name_scope('Accuracy'):\n", | |
"# correct_prediction = tf.equal(tf.argmax(prob, 1), 0)\n", | |
"# accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", | |
"# tf.scalar_summary('accuracy', accuracy)\n", | |
"\n", | |
"merged = tf.merge_all_summaries()\n", | |
"\n", | |
"with tf.name_scope('Test'):\n", | |
" average_loss = tf.placeholder(tf.float32)\n", | |
" loss_summary = tf.scalar_summary('average_loss', average_loss)\n", | |
"\n", | |
"\n", | |
"def pull_batch(query_data, doc_data, batch_idx):\n", | |
" start = time.time()\n", | |
" print(batch_idx * BS,(batch_idx + 1) * BS ,query_data.shape)\n", | |
" query_in = query_data[batch_idx * BS:(batch_idx + 1) * BS, :]\n", | |
" doc_in = doc_data[batch_idx * BS:(batch_idx + 1) * BS, :]\n", | |
" query_in = query_in.tocoo()\n", | |
" doc_in = doc_in.tocoo()\n", | |
"\n", | |
" query_in = tf.SparseTensorValue(\n", | |
" np.transpose([np.array(query_in.row, dtype=np.int64), np.array(query_in.col, dtype=np.int64)]),\n", | |
" np.array(query_in.data, dtype=np.float), np.array(query_in.shape, dtype=np.int64))\n", | |
" doc_in = tf.SparseTensorValue(\n", | |
" np.transpose([np.array(doc_in.row, dtype=np.int64), np.array(doc_in.col, dtype=np.int64)]),\n", | |
" np.array(doc_in.data, dtype=np.float), np.array(doc_in.shape, dtype=np.int64))\n", | |
"\n", | |
" end = time.time()\n", | |
" print(\"Pull_batch time: %f\" % (end - start))\n", | |
"\n", | |
" return query_in, doc_in\n", | |
"\n", | |
"\n", | |
"def feed_dict(Train, batch_idx):\n", | |
" \"\"\"Make a TensorFlow feed_dict: maps data onto Tensor placeholders.\"\"\"\n", | |
" if Train:\n", | |
" query_in, doc_in = pull_batch(query_train_data, doc_train_data, batch_idx)\n", | |
" else:\n", | |
" query_in, doc_in = pull_batch(query_test_data, doc_test_data, batch_idx)\n", | |
" return {query_batch: query_in, doc_batch: doc_in}\n", | |
"\n", | |
"\n", | |
"config = tf.ConfigProto() # log_device_placement=True)\n", | |
"config.gpu_options.allow_growth = True\n", | |
"# if not FLAGS.gpu:\n", | |
"# config = tf.ConfigProto(device_count= {'GPU' : 0})\n", | |
"\n", | |
"with tf.Session(config=config) as sess:\n", | |
" sess.run(tf.initialize_all_variables())\n", | |
" train_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/train', sess.graph)\n", | |
" test_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/test', sess.graph)\n", | |
"\n", | |
" # Actual execution\n", | |
" start = time.time()\n", | |
" # fp_time = 0\n", | |
" # fbp_time = 0\n", | |
" for step in range(FLAGS.max_steps):\n", | |
" print(\"Step %s\" % step)\n", | |
" batch_idx = 1 #step % FLAGS.epoch_steps\n", | |
" if batch_idx % FLAGS.pack_size == 0:\n", | |
" load_train_data(batch_idx / FLAGS.pack_size + 1)\n", | |
"\n", | |
" # # setup toolbar\n", | |
" # sys.stdout.write(\"[%s]\" % (\" \" * toolbar_width))\n", | |
" # #sys.stdout.flush()\n", | |
" # sys.stdout.write(\"\\b\" * (toolbar_width + 1)) # return to start of line, after '['\n", | |
"\n", | |
" if batch_idx % (FLAGS.pack_size / 64) == 0:\n", | |
" progress = 100.0 * batch_idx / FLAGS.epoch_steps\n", | |
" sys.stdout.write(\"\\r%.2f%% Epoch\\n\" % progress)\n", | |
" sys.stdout.flush()\n", | |
"\n", | |
" # t1 = time.time()\n", | |
" # sess.run(loss, feed_dict = feed_dict(True, batch_idx))\n", | |
" # t2 = time.time()\n", | |
" # fp_time += t2 - t1\n", | |
" # #print(t2-t1)\n", | |
" # t1 = time.time()\n", | |
" sess.run(train_step, feed_dict=feed_dict(True, batch_idx % FLAGS.pack_size))\n", | |
" # t2 = time.time()\n", | |
" # fbp_time += t2 - t1\n", | |
" # #print(t2 - t1)\n", | |
" # if batch_idx % 2000 == 1999:\n", | |
" # print (\"MiniBatch: Average FP Time %f, Average FP+BP Time %f\" %\n", | |
" # (fp_time / step, fbp_time / step))\n", | |
"\n", | |
"\n", | |
" if batch_idx == FLAGS.epoch_steps - 1:\n", | |
" end = time.time()\n", | |
" epoch_loss = 0\n", | |
" for i in range(FLAGS.pack_size):\n", | |
" print(\"First Pack Size %s\" % i)\n", | |
" loss_v = sess.run(loss, feed_dict=feed_dict(True, i))\n", | |
" epoch_loss += loss_v\n", | |
"\n", | |
" epoch_loss /= FLAGS.pack_size\n", | |
" train_loss = sess.run(loss_summary, feed_dict={average_loss: epoch_loss})\n", | |
" train_writer.add_summary(train_loss, step + 1)\n", | |
"\n", | |
" # print (\"MiniBatch: Average FP Time %f, Average FP+BP Time %f\" %\n", | |
" # (fp_time / step, fbp_time / step))\n", | |
" #\n", | |
" print (\"\\nEpoch #%-5d | Train Loss: %-4.3f | PureTrainTime: %-3.3fs\" %\n", | |
" (step / FLAGS.epoch_steps, epoch_loss, end - start))\n", | |
"\n", | |
" epoch_loss = 0\n", | |
" for i in range(FLAGS.pack_size):\n", | |
" print(\"Second Pack Size %s\" % i)\n", | |
" loss_v = sess.run(loss, feed_dict=feed_dict(False, i))\n", | |
" epoch_loss += loss_v\n", | |
"\n", | |
" epoch_loss /= FLAGS.pack_size\n", | |
"\n", | |
" test_loss = sess.run(loss_summary, feed_dict={average_loss: epoch_loss})\n", | |
" test_writer.add_summary(test_loss, step + 1)\n", | |
"\n", | |
" start = time.time()\n", | |
" print (\"Epoch #%-5d | Test Loss: %-4.3f | Calc_LossTime: %-3.3fs\" %\n", | |
" (step / FLAGS.epoch_steps, epoch_loss, start - end))\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python [Root]", | |
"language": "python", | |
"name": "Python [Root]" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment