facuq

[tensorflow] Computer freezes when feeding a large np array

Dec 1st, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1.     from tensorflow.examples.tutorials.mnist import input_data
  2. import tensorflow as tf
  3.  
  4. mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
  5. sess = tf.InteractiveSession()
  6.  
  7. x = tf.placeholder(tf.float32, shape=[None, 784])
  8. y_ = tf.placeholder(tf.float32, shape=[None, 10])
  9.  
  10. def weight_variable(shape):
  11.   initial = tf.truncated_normal(shape, stddev=0.1)
  12.   return tf.Variable(initial)
  13.  
  14. def bias_variable(shape):
  15.   initial = tf.constant(0.1, shape=shape)
  16.   return tf.Variable(initial)
  17.  
  18. def conv2d(x, W):
  19.   return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
  20.  
  21. def max_pool_2x2(x):
  22.   return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
  23.                         strides=[1, 2, 2, 1], padding='SAME')
  24.  
  25.  
  26. W_conv1 = weight_variable([5, 5, 1, 32])
  27. b_conv1 = bias_variable([32])
  28.  
  29. x_image = tf.reshape(x, [-1,28,28,1])
  30.  
  31. h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
  32. h_pool1 = max_pool_2x2(h_conv1)
  33.  
  34. W_conv2 = weight_variable([5, 5, 32, 64])
  35. b_conv2 = bias_variable([64])
  36.  
  37. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
  38. h_pool2 = max_pool_2x2(h_conv2)
  39.  
  40. W_fc1 = weight_variable([7 * 7 * 64, 1024])
  41. b_fc1 = bias_variable([1024])
  42.  
  43. h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
  44. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
  45.  
  46. keep_prob = tf.placeholder(tf.float32)
  47. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  48.  
  49. W_fc2 = weight_variable([1024, 10])
  50. b_fc2 = bias_variable([10])
  51.  
  52. y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
  53.  
  54. cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
  55. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  56. correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  57. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  58. sess.run(tf.initialize_all_variables())
  59.  
  60. for i in range(20000):
  61.   batch = mnist.train.next_batch(50)
  62.   print(batch)
  63.   if i%100 == 0:
  64.     # train_accuracy = accuracy.eval(feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0})
  65.     train_accuracy = accuracy.eval(feed_dict={ x:mnist.train.images, y_: mnist.train.labels, keep_prob: 1.0})
  66.     print("step %d, training accuracy %g"%(i, train_accuracy))
  67.   train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
  68.  
  69. print("test accuracy %g"%accuracy.eval(feed_dict={
  70.     x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
Add Comment
Please, Sign In to add comment