├── README.md ├── Very-Deep-Convolutional-Networks-for-Text-Classification.ipynb └── test_accuracy.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Very-Deep-Convolutional-Networks-for-Natural-Language-Processing-in-tensorflow 2 | Implement the paper" Very Deep Convolutional Networks for Natural Language Processing"(https://arxiv.org/abs/1606.01781) in tensorflow,just 9 layers. 3 | 4 | Parts of code are based on https://github.com/amygdala/tensorflow-workshop/tree/master/workshop_sections/cnn_text_classification ,which is based on the https://github.com/dennybritz/cnn-text-classification-tf, and other parts are based on https://github.com/scharmchi/char-level-cnn-tf 5 | 6 | The data of the experiment is dbpedia, the paper reports that the accuracy is 0.9865. 7 | -------------------------------------------------------------------------------- /Very-Deep-Convolutional-Networks-for-Text-Classification.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import csv\n", 12 | "import numpy as np\n", 13 | "samples = {}\n", 14 | "ALPHABET = \"abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\\\"/\\\\|_@#$%^&*~`+ =<>()[]{}\"\n", 15 | "FEATURE_LEN =1014\n", 16 | "cdict = {}\n", 17 | "for i,c in enumerate(ALPHABET):\n", 18 | " cdict[c] = i + 2" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [], 28 | "source": [ 29 | "samples = {}\n", 30 | "with open('/dbpedia_data/train.csv') as f:\n", 31 | " reader = csv.DictReader(f,fieldnames=['class'],restkey='fields')\n", 32 | " for row in reader:\n", 33 | " label = row['class']\n", 34 | " if label not in samples:\n", 35 | " samples[label] = []\n", 36 | " sample = np.ones(FEATURE_LEN)\n", 37 | " count = 0\n", 38 | " for field in row['fields']:\n", 39 | " for char in field.lower():\n", 40 | " if char in cdict:\n", 41 | " sample[count] = cdict[char]\n", 42 | " count += 1\n", 43 | " if count >= FEATURE_LEN-1:\n", 44 | " break\n", 45 | " samples[label].append(sample)\n", 46 | " samples_per_class = None\n", 47 | " classes = samples.keys()\n", 48 | " class_samples = []\n", 49 | " for c in classes:\n", 50 | " if samples_per_class is None:\n", 51 | " samples_per_class = len(samples[c])\n", 52 | " else:\n", 53 | " assert samples_per_class == len(samples[c])\n", 54 | " class_samples.append(samples[c])" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": { 61 | "collapsed": false 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "def build_onehot(input_):\n", 66 | " target = np.zeros(len(classes))\n", 67 | " target[input_] = 1\n", 68 | " return target\n", 69 | "y= []\n", 70 | "for i in range(len(classes)):\n", 71 | " for j in range(samples_per_class):\n", 72 | " target =build_onehot(i)\n", 73 | " y.append(target)" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": { 80 | "collapsed": true 81 | }, 82 | "outputs": [], 83 | "source": [ 84 | "x = np.reshape(class_samples,(-1,FEATURE_LEN))" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": { 91 | "collapsed": true 92 | }, 93 | "outputs": [], 94 | "source": [ 95 | "y = np.reshape(y,(-1,len(classes)))" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": { 102 | "collapsed": false 103 | }, 104 | "outputs": [], 105 | "source": [ 106 | "np.random.seed(10)\n", 107 | "shuffle_indices = np.random.permutation(np.arange(len(y)))\n", 108 | "x_shuffled = x[shuffle_indices]\n", 109 | "y_shuffled = y[shuffle_indices]\n", 110 | "# Split train/test set\n", 111 | "n_dev_samples = 10000\n", 112 | "# TODO: Create a fuckin' correct cross validation procedure\n", 113 | "x_train, x_dev = x_shuffled[:-n_dev_samples], x_shuffled[-n_dev_samples:]\n", 114 | "y_train, y_dev = y_shuffled[:-n_dev_samples], y_shuffled[-n_dev_samples:]\n", 115 | "print(\"Train/Dev split: {:d}/{:d}\".format(len(y_train), len(y_dev)))" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "def Convolutional_Block(input_,filter_num,train,scope):\n", 127 | " norm = tf.random_normal_initializer(stddev=0.05)\n", 128 | " const = tf.constant_initializer(0.0)\n", 129 | " filter_shape1 = [3, 1, input_.get_shape()[3], filter_num]\n", 130 | " \n", 131 | " with tf.variable_scope(scope):\n", 132 | " filter_1 = tf.get_variable('filter1', filter_shape1, initializer=norm)\n", 133 | " conv1 = tf.nn.conv2d(input_, filter_1, strides=[1, 1, filter_shape1[1], 1], padding=\"SAME\")\n", 134 | " batch_normal1 =tflearn.layers.normalization.batch_normalization(conv1,trainable= train,scope = scope+\"BN1\")\n", 135 | " filter_shape2 = [3, 1, batch_normal1.get_shape()[3], filter_num]\n", 136 | " filter_2 = tf.get_variable('fileer2', filter_shape2, initializer=norm)\n", 137 | " conv2 = tf.nn.conv2d(tf.nn.relu(batch_normal1), filter_2, strides=[1, 1, filter_shape2[1], 1], padding=\"SAME\")\n", 138 | " batch_normal2 =tflearn.layers.normalization.batch_normalization(conv2,trainable=train,scope = scope+\"BN2\")\n", 139 | " pooled = tf.nn.max_pool(tf.nn.relu(batch_normal2),ksize=[1, 3, 1, 1],strides=[1, 2, 1, 1],padding='SAME',name=\"pool1\")\n", 140 | " return pooled\n", 141 | "def Conv(input_,filter_shape,strides,train,scope):\n", 142 | " norm = tf.random_normal_initializer(stddev=0.05)\n", 143 | " const = tf.constant_initializer(0.0)\n", 144 | " with tf.variable_scope(scope):\n", 145 | " filter_1 = tf.get_variable('filter1', filter_shape, initializer=norm)\n", 146 | " conv = tf.nn.conv2d(input_, filter_1, strides=strides, padding=\"SAME\")\n", 147 | " batch_normal =tflearn.layers.normalization.batch_normalization(conv,trainable=train,scope = scope+\"BN\")\n", 148 | " return batch_normal\n", 149 | "def linear(input, output_dim, scope=None, stddev=0.1):\n", 150 | " norm = tf.random_normal_initializer(stddev=stddev)\n", 151 | " const = tf.constant_initializer(0.0)\n", 152 | " with tf.variable_scope(scope or 'linear'):\n", 153 | " w = tf.get_variable('w', [input.get_shape()[1], output_dim], initializer=norm)\n", 154 | " b = tf.get_variable('b', [output_dim], initializer=const)\n", 155 | " l2_loss = tf.nn.l2_loss(w)+tf.nn.l2_loss(b)\n", 156 | " return tf.matmul(input, w) + b,l2_loss" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": { 163 | "collapsed": false 164 | }, 165 | "outputs": [], 166 | "source": [ 167 | "import tensorflow as tf\n", 168 | "from inception.slim import ops\n", 169 | "import tflearn\n", 170 | "class CharCNN(object):\n", 171 | " \"\"\"\n", 172 | " A CNN for text classification.\n", 173 | " based on the Very Deep Convolutional Networks for Natural Language Processing.\n", 174 | " \"\"\"\n", 175 | " def __init__(self, num_classes=14, filter_size=3,\n", 176 | " l2_reg_lambda=0.001, sequence_max_length=1014, num_quantized_chars=71,embedding_size=16):\n", 177 | " \n", 178 | " self.input_x = tf.placeholder(tf.int32, [None,sequence_max_length], name=\"input_x\")\n", 179 | " self.input_y = tf.placeholder(tf.float32, [None, num_classes], name=\"input_y\")\n", 180 | " self.dropout_keep_prob = tf.placeholder(tf.float32, name=\"dropout_keep_prob\")\n", 181 | " self.training = tf.placeholder(tf.int32, name=\"trainable\")\n", 182 | " if self.training==1:\n", 183 | " TRAIN = True\n", 184 | " else:\n", 185 | " TRAIN = False\n", 186 | " l2_loss = tf.constant(0.0)\n", 187 | " with tf.device('/cpu:0'),tf.name_scope(\"embedding\"):\n", 188 | " W0 = tf.Variable(tf.random_uniform([num_quantized_chars, embedding_size], -1.0, 1.0),name=\"W\")\n", 189 | " self.embedded_characters = tf.nn.embedding_lookup(W0,self.input_x)\n", 190 | " self.embedded_characters_expanded = tf.expand_dims(self.embedded_characters,-1,name=\"embedding_input\")\n", 191 | " with tf.name_scope(\"layer-0\"): \n", 192 | " filter_shape0 = [3, embedding_size, 1, 64]\n", 193 | " strides0 =[1, 1,embedding_size, 1]\n", 194 | " self.h0 = Conv(self.embedded_characters_expanded,filter_shape0,strides0,TRAIN,'layer_0')\n", 195 | " with tf.name_scope(\"layer-1-2-3-4-5-6-7-8\"):\n", 196 | " self.h1 = Convolutional_Block(self.h0,64,TRAIN,'layer_1-2')\n", 197 | " self.h2 = Convolutional_Block(self.h1,128,TRAIN,'layer_3-4')\n", 198 | " self.h3 = Convolutional_Block(self.h2,256,TRAIN,'layer_5-6')\n", 199 | " self.h4 = Convolutional_Block(self.h3,512,TRAIN,'layer_7-8')\n", 200 | " self.h5 = tf.transpose(self.h4,[0,3,2,1])\n", 201 | " self.pooled = tf.nn.top_k(self.h5, k=8,name='k-maxpooling') \n", 202 | " self.h6 = tf.reshape(self.pooled[0],(-1,512*8))\n", 203 | " with tf.name_scope(\"fc-1-2-3\"):\n", 204 | " self.fc1_out,fc1_loss = linear(self.h6, 2048, scope='fc1', stddev=0.1)\n", 205 | " l2_loss += fc1_loss\n", 206 | " self.fc2_out,fc2_loss = linear(tf.nn.relu(self.fc1_out), 2048, scope='fc2', stddev=0.1)\n", 207 | " l2_loss += fc2_loss\n", 208 | " self.fc3_out,fc3_loss = linear(tf.nn.relu(self.fc2_out), num_classes, scope='fc3', stddev=0.1)\n", 209 | " l2_loss += fc3_loss\n", 210 | " with tf.name_scope(\"loss\"):\n", 211 | " self.predictions = tf.argmax(self.fc3_out, 1, name=\"predictions\")\n", 212 | " losses = tf.nn.softmax_cross_entropy_with_logits(logits = self.fc3_out,labels = self.input_y)\n", 213 | " self.loss = tf.reduce_mean(losses) + l2_reg_lambda * l2_loss\n", 214 | " with tf.name_scope(\"accuracy\"):\n", 215 | " correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))\n", 216 | " self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, \"float\"), name=\"accuracy\")" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": null, 222 | "metadata": { 223 | "collapsed": true 224 | }, 225 | "outputs": [], 226 | "source": [ 227 | "x_batch = x_train[0:64]\n", 228 | "y_batch = y_train[0:64]" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "metadata": { 235 | "collapsed": false, 236 | "scrolled": true 237 | }, 238 | "outputs": [], 239 | "source": [ 240 | "import os\n", 241 | "import tensorflow as tf\n", 242 | "os.environ.setdefault('CUDA_VISIBLE_DEVICES','3')\n", 243 | "gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)\n", 244 | "with tf.Graph().as_default():\n", 245 | " session_conf = tf.ConfigProto(gpu_options = gpu_options,\n", 246 | " allow_soft_placement=True,\n", 247 | " log_device_placement=False)\n", 248 | " sess = tf.Session(config=session_conf)\n", 249 | " with sess.as_default():\n", 250 | " cnn = CharCNN()\n", 251 | " sess.run(tf.initialize_all_variables())\n", 252 | " feed_dict = {\n", 253 | " cnn.input_x: x_batch,\n", 254 | " cnn.input_y: y_batch,\n", 255 | " cnn.training:1,\n", 256 | " cnn.dropout_keep_prob: 0.5\n", 257 | " } \n", 258 | " out = sess.run(cnn.accuracy,feed_dict)" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "metadata": { 265 | "collapsed": true 266 | }, 267 | "outputs": [], 268 | "source": [ 269 | "import os\n", 270 | "import time\n", 271 | "import datetime\n", 272 | "def batch_iter(x, y, batch_size, num_epochs, shuffle=True):\n", 273 | " \"\"\"\n", 274 | " Generates a batch iterator for a dataset.\n", 275 | " \"\"\"\n", 276 | " # data = np.array(data)\n", 277 | " data_size = len(x)\n", 278 | " num_batches_per_epoch = int(data_size/batch_size) + 1\n", 279 | " for epoch in range(num_epochs):\n", 280 | " print(\"In epoch >> \" + str(epoch + 1))\n", 281 | " print(\"num batches per epoch is: \" + str(num_batches_per_epoch))\n", 282 | " # Shuffle the data at each epoch\n", 283 | " if shuffle:\n", 284 | " shuffle_indices = np.random.permutation(np.arange(data_size))\n", 285 | " x_shuffled = x[shuffle_indices]\n", 286 | " y_shuffled = y[shuffle_indices]\n", 287 | " else:\n", 288 | " x_shuffled = x\n", 289 | " y_shuffled = y\n", 290 | " for batch_num in range(num_batches_per_epoch-1):\n", 291 | " start_index = batch_num * batch_size\n", 292 | " end_index = min((batch_num + 1) * batch_size, data_size)\n", 293 | " x_batch = x_shuffled[start_index:end_index]\n", 294 | " y_batch = y_shuffled[start_index:end_index]\n", 295 | " batch = list(zip(x_batch, y_batch))\n", 296 | " yield batch" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": null, 302 | "metadata": { 303 | "collapsed": false, 304 | "scrolled": true 305 | }, 306 | "outputs": [], 307 | "source": [ 308 | "batch_size =128\n", 309 | "num_epochs = 20\n", 310 | "with tf.Graph().as_default():\n", 311 | " session_conf = tf.ConfigProto(\n", 312 | " allow_soft_placement=True,\n", 313 | " log_device_placement=False)\n", 314 | " sess = tf.Session(config=session_conf)\n", 315 | " with sess.as_default():\n", 316 | " cnn = CharCNN()\n", 317 | " # Define Training procedure\n", 318 | " global_step = tf.Variable(0, name=\"global_step\", trainable=False)\n", 319 | " optimizer = tf.train.AdamOptimizer(1e-3)\n", 320 | " grads_and_vars = optimizer.compute_gradients(cnn.loss)\n", 321 | " train_op = optimizer.apply_gradients(\n", 322 | " grads_and_vars, global_step=global_step)\n", 323 | "\n", 324 | " # Keep track of gradient values and sparsity (optional)\n", 325 | " grad_summaries = []\n", 326 | " for g, v in grads_and_vars:\n", 327 | " if g is not None:\n", 328 | " grad_hist_summary = tf.summary.histogram(\n", 329 | " \"{}/grad/hist\".format(v.name), g)\n", 330 | " sparsity_summary = tf.summary.scalar(\n", 331 | " \"{}/grad/sparsity\".format(v.name), tf.nn.zero_fraction(g))\n", 332 | " grad_summaries.append(grad_hist_summary)\n", 333 | " grad_summaries.append(sparsity_summary)\n", 334 | " grad_summaries_merged = tf.summary.merge(grad_summaries)\n", 335 | "\n", 336 | " # Output directory for models and summaries\n", 337 | " timestamp = str(int(time.time()))\n", 338 | " out_dir = os.path.abspath(os.path.join(\n", 339 | " os.path.curdir, \"runs_new\", timestamp))\n", 340 | " print(\"Writing to {}\\n\".format(out_dir))\n", 341 | "\n", 342 | " # Summaries for loss and accuracy\n", 343 | " loss_summary = tf.summary.scalar(\"loss\", cnn.loss)\n", 344 | " acc_summary = tf.summary.scalar(\"accuracy\", cnn.accuracy)\n", 345 | "\n", 346 | " # Train Summaries\n", 347 | " train_summary_op = tf.summary.merge(\n", 348 | " [loss_summary, acc_summary, grad_summaries_merged])\n", 349 | " train_summary_dir = os.path.join(out_dir, \"summaries\", \"train\")\n", 350 | " train_summary_writer = tf.summary.FileWriter(\n", 351 | " train_summary_dir, sess.graph)\n", 352 | "\n", 353 | " # Dev summaries\n", 354 | " dev_summary_op = tf.summary.merge([loss_summary, acc_summary])\n", 355 | " dev_summary_dir = os.path.join(out_dir, \"summaries\", \"dev\")\n", 356 | " dev_summary_writer = tf.summary.FileWriter(dev_summary_dir, sess.graph)\n", 357 | "\n", 358 | " # Checkpoint directory. Tensorflow assumes this directory already\n", 359 | " # exists, so we need to create it.\n", 360 | " checkpoint_dir = os.path.abspath(os.path.join(out_dir, \"checkpoints\"))\n", 361 | " checkpoint_prefix = os.path.join(checkpoint_dir, \"model\")\n", 362 | " if not os.path.exists(checkpoint_dir):\n", 363 | " os.makedirs(checkpoint_dir)\n", 364 | " saver = tf.train.Saver(tf.all_variables())\n", 365 | "\n", 366 | " # Initialize all variables\n", 367 | " sess.run(tf.initialize_all_variables())\n", 368 | " def train_step(x_batch, y_batch):\n", 369 | " \"\"\"\n", 370 | " A single training step\n", 371 | " \"\"\"\n", 372 | " feed_dict = {\n", 373 | " cnn.input_x: x_batch,\n", 374 | " cnn.input_y: y_batch,\n", 375 | " cnn.training:1,\n", 376 | " cnn.dropout_keep_prob: 0.5\n", 377 | " } \n", 378 | " _, step, summaries, loss, accuracy = sess.run(\n", 379 | " [train_op, global_step, train_summary_op,\n", 380 | " cnn.loss, cnn.accuracy],\n", 381 | " feed_dict)\n", 382 | " time_str = datetime.datetime.now().isoformat()\n", 383 | " # write fewer training summaries, to keep events file from\n", 384 | " # growing so big.\n", 385 | " if step % (evaluate_every / 2) == 0:\n", 386 | " print(\"{}: step {}, loss {:g}, acc {:g}\".format(\n", 387 | " time_str, step, loss, accuracy))\n", 388 | " train_summary_writer.add_summary(summaries, step)\n", 389 | " def dev_step(x_batch, y_batch, writer=None):\n", 390 | " dev_size = len(x_batch)\n", 391 | " max_batch_size = 500\n", 392 | " num_batches = dev_size/max_batch_size\n", 393 | " acc = []\n", 394 | " losses = []\n", 395 | " print(\"Number of batches in dev set is \" + str(num_batches))\n", 396 | " for i in range(num_batches):\n", 397 | " x_batch_dev = x_batch[i * max_batch_size:(i + 1) * max_batch_size]\n", 398 | " y_batch_dev = y_batch[i * max_batch_size: (i + 1) * max_batch_size]\n", 399 | " feed_dict = {\n", 400 | " cnn.input_x: x_batch_dev,\n", 401 | " cnn.input_y: y_batch_dev,\n", 402 | " cnn.training:0,\n", 403 | " cnn.dropout_keep_prob: 1.0\n", 404 | " }\n", 405 | " step, summaries, loss, accuracy = sess.run(\n", 406 | " [global_step, dev_summary_op, cnn.loss, cnn.accuracy],\n", 407 | " feed_dict)\n", 408 | " acc.append(accuracy)\n", 409 | " losses.append(loss)\n", 410 | " time_str = datetime.datetime.now().isoformat()\n", 411 | " print(\"batch \" + str(i + 1) + \" in dev >>\" +\" {}: loss {:g}, acc {:g}\".format(time_str, loss, accuracy))\n", 412 | " if writer:\n", 413 | " writer.add_summary(summaries, step)\n", 414 | " print(\"\\nMean accuracy=\" + str(sum(acc)/len(acc)))\n", 415 | " print(\"Mean loss=\" + str(sum(losses)/len(losses)))\n", 416 | " # just for epoch counting\n", 417 | " num_batches_per_epoch = int(len(x_train)/batch_size) + 1\n", 418 | " # Generate batches\n", 419 | " batches = batch_iter(x_train, y_train,batch_size, num_epochs)\n", 420 | " evaluate_every = 300\n", 421 | " checkpoint_every =300\n", 422 | " # Training loop. For each batch...\n", 423 | " for batch in batches:\n", 424 | " x_batch, y_batch = zip(*batch)\n", 425 | " train_step(x_batch, y_batch)\n", 426 | " current_step = tf.train.global_step(sess, global_step)\n", 427 | " if current_step % evaluate_every == 0:\n", 428 | " print(\"\\nEvaluation:\")\n", 429 | " dev_step(x_dev, y_dev, writer=dev_summary_writer)\n", 430 | " print(\"Epoch: {}\".format(\n", 431 | " int(current_step / num_batches_per_epoch)))\n", 432 | " print(\"\")\n", 433 | " if current_step % checkpoint_every == 0:\n", 434 | " path = saver.save(\n", 435 | " sess, checkpoint_prefix, global_step=current_step)\n", 436 | " print(\"Saved model checkpoint to {}\\n\".format(path))" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": null, 442 | "metadata": { 443 | "collapsed": true 444 | }, 445 | "outputs": [], 446 | "source": [] 447 | } 448 | ], 449 | "metadata": { 450 | "kernelspec": { 451 | "display_name": "Python 2", 452 | "language": "python", 453 | "name": "python2" 454 | }, 455 | "language_info": { 456 | "codemirror_mode": { 457 | "name": "ipython", 458 | "version": 2 459 | }, 460 | "file_extension": ".py", 461 | "mimetype": "text/x-python", 462 | "name": "python", 463 | "nbconvert_exporter": "python", 464 | "pygments_lexer": "ipython2", 465 | "version": "2.7.9" 466 | } 467 | }, 468 | "nbformat": 4, 469 | "nbformat_minor": 0 470 | } 471 | -------------------------------------------------------------------------------- /test_accuracy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 21, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "import tensorflow as tf\n", 13 | "# Load data. Load your own data here\n", 14 | "import csv\n", 15 | "import numpy as np\n", 16 | "samples = {}\n", 17 | "ALPHABET = \"abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\\\"/\\\\|_@#$%^&*~`+ =<>()[]{}\"\n", 18 | "FEATURE_LEN =1014\n", 19 | "cdict = {}\n", 20 | "for i,c in enumerate(ALPHABET):\n", 21 | " cdict[c] = i + 2\n", 22 | "samples = {}\n", 23 | "with open('/dbpedia_data/test.csv') as f:\n", 24 | " reader = csv.DictReader(f,fieldnames=['class'],restkey='fields')\n", 25 | " for row in reader:\n", 26 | " label = row['class']\n", 27 | " if label not in samples:\n", 28 | " samples[label] = []\n", 29 | " sample = np.ones(FEATURE_LEN)\n", 30 | " count = 0\n", 31 | " for field in row['fields']:\n", 32 | " for char in field.lower():\n", 33 | " if char in cdict:\n", 34 | " sample[count] = cdict[char]\n", 35 | " count += 1\n", 36 | " if count >= FEATURE_LEN-1:\n", 37 | " break\n", 38 | " samples[label].append(sample)\n", 39 | " samples_per_class = None\n", 40 | " classes = samples.keys()\n", 41 | " class_samples = []\n", 42 | " for c in classes:\n", 43 | " if samples_per_class is None:\n", 44 | " samples_per_class = len(samples[c])\n", 45 | " else:\n", 46 | " assert samples_per_class == len(samples[c])\n", 47 | " class_samples.append(samples[c])\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 22, 53 | "metadata": { 54 | "collapsed": true 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "def build_onehot(input_):\n", 59 | " target = np.zeros(len(classes))\n", 60 | " target[input_] = 1\n", 61 | " return target\n", 62 | "y= []\n", 63 | "y_ = []\n", 64 | "for i in range(len(classes)):\n", 65 | " for j in range(samples_per_class):\n", 66 | " target =build_onehot(i)\n", 67 | " y.append(target)\n", 68 | " y_.append(i)\n", 69 | "y_ = np.array(y_)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 23, 75 | "metadata": { 76 | "collapsed": true 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "x = np.reshape(class_samples,(-1,FEATURE_LEN))\n", 81 | "y = np.reshape(y,(-1,len(classes)))" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 31, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "\n", 96 | "Evaluating...\n", 97 | "\n", 98 | "checkpoint file: None\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "print(\"\\nEvaluating...\\n\")\n", 104 | "# Evaluation\n", 105 | "# ==================================================\n", 106 | "checkpoint_dir = '/runs_new/1490880156/checkpoints/'\n", 107 | "checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)\n", 108 | "print(\"checkpoint file: {}\".format(checkpoint_file))" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 25, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "import os\n", 120 | "import time\n", 121 | "import datetime\n", 122 | "def batch_iter(x, y, batch_size, num_epochs, shuffle=False):\n", 123 | " \"\"\"\n", 124 | " Generates a batch iterator for a dataset.\n", 125 | " \"\"\"\n", 126 | " # data = np.array(data)\n", 127 | " data_size = len(x)\n", 128 | " num_batches_per_epoch = int(data_size/batch_size) + 1\n", 129 | " print(\"num batches per epoch is: \" + str(num_batches_per_epoch))\n", 130 | " # Shuffle the data at each epoch\n", 131 | " if shuffle:\n", 132 | " shuffle_indices = np.random.permutation(np.arange(data_size))\n", 133 | " x_shuffled = x[shuffle_indices]\n", 134 | " y_shuffled = y[shuffle_indices]\n", 135 | " else:\n", 136 | " x_shuffled = x\n", 137 | " y_shuffled = y\n", 138 | " for batch_num in range(num_batches_per_epoch):\n", 139 | " start_index = batch_num * batch_size\n", 140 | " end_index = min((batch_num + 1) * batch_size, data_size)\n", 141 | " x_batch = x_shuffled[start_index:end_index]\n", 142 | " y_batch = y_shuffled[start_index:end_index]\n", 143 | " batch = list(zip(x_batch, y_batch))\n", 144 | " yield batch" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 26, 150 | "metadata": { 151 | "collapsed": false 152 | }, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "num batches per epoch is: 547\n", 159 | "Total number of test examples: 70000\n", 160 | "Accuracy: 0.920271\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "graph = tf.Graph()\n", 166 | "with graph.as_default():\n", 167 | " session_conf = tf.ConfigProto(\n", 168 | " allow_soft_placement=True,\n", 169 | " log_device_placement=False)\n", 170 | " sess = tf.Session(config=session_conf)\n", 171 | " with sess.as_default():\n", 172 | " # Load the saved meta graph and restore variables\n", 173 | " saver = tf.train.import_meta_graph(\"{}.meta\".format(checkpoint_file))\n", 174 | " saver.restore(sess, checkpoint_file)\n", 175 | "\n", 176 | " # Get the placeholders from the graph by name\n", 177 | " input_x = graph.get_operation_by_name(\"input_x\").outputs[0]\n", 178 | " dropout_keep_prob = graph.get_operation_by_name(\n", 179 | " \"dropout_keep_prob\").outputs[0]\n", 180 | "\n", 181 | " # Tensors we want to evaluate\n", 182 | " predictions = graph.get_operation_by_name(\n", 183 | " \"loss/predictions\").outputs[0]\n", 184 | "\n", 185 | " # Generate batches for one epoch\n", 186 | " \n", 187 | " batches = batch_iter(x, y,128, 1)\n", 188 | "\n", 189 | " # Collect the predictions here\n", 190 | " all_predictions = []\n", 191 | "\n", 192 | " for test_batch in batches:\n", 193 | " x_batch, y_batch = zip(*test_batch)\n", 194 | " batch_predictions = sess.run(\n", 195 | " predictions, {input_x: x_batch, dropout_keep_prob: 1.0})\n", 196 | " all_predictions = np.concatenate(\n", 197 | " [all_predictions, batch_predictions])\n", 198 | "\n", 199 | "# Print accuracy\n", 200 | "correct_predictions = float(sum(np.equal(all_predictions,y_)))\n", 201 | "print(\"Total number of test examples: {}\".format(len(y_)))\n", 202 | "print(\"Accuracy: {:g}\".format(correct_predictions/float(len(y_))))" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": null, 208 | "metadata": { 209 | "collapsed": true 210 | }, 211 | "outputs": [], 212 | "source": [] 213 | } 214 | ], 215 | "metadata": { 216 | "kernelspec": { 217 | "display_name": "Python 2", 218 | "language": "python", 219 | "name": "python2" 220 | }, 221 | "language_info": { 222 | "codemirror_mode": { 223 | "name": "ipython", 224 | "version": 2 225 | }, 226 | "file_extension": ".py", 227 | "mimetype": "text/x-python", 228 | "name": "python", 229 | "nbconvert_exporter": "python", 230 | "pygments_lexer": "ipython2", 231 | "version": "2.7.9" 232 | } 233 | }, 234 | "nbformat": 4, 235 | "nbformat_minor": 0 236 | } 237 | --------------------------------------------------------------------------------