Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import tensorflow as tf
  2. from tensorflow.examples.tutorials.mnist import input_data
  3. mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)
  4.  
  5. batch_size = 100
  6.  
  7. #MODEL
  8.  
  9. def neural_network_model(data):
  10. weights = {'W_conv': tf.Variable(tf.random_normal([5,5,1,32])),
  11. 'W_fc': tf.Variable(tf.random_normal([14*14*32,100])),
  12. 'W_out': tf.Variable(tf.random_normal([100,10])),}
  13.  
  14.  
  15. biases = {'b_conv':tf.Variable(tf.random_normal([32])),
  16. 'b_fc':tf.Variable(tf.random_normal([100])),
  17. 'b_out':tf.Variable(tf.random_normal([10])),
  18. }
  19.  
  20. x = tf.reshape(data,shape=[-1,28,28,1])
  21.  
  22. c = tf.nn.conv2d(x,weights['W_conv'], strides = [1,1,1,1],padding= 'SAME')
  23. c += biases["b_conv"]
  24. c = tf.nn.relu(c)
  25.  
  26. m = tf.nn.max_pool(c,ksize=[1,2,2,1], strides = [1,2,2,1], padding = "SAME")
  27.  
  28.  
  29. fc = tf.reshape(m,[-1,14*14*32])
  30. fc = tf.nn.relu(tf.add(tf.matmul(fc,weights['W_fc']),biases["b_fc"]))
  31.  
  32. output = tf.nn.relu(tf.matmul(fc,weights['W_out'])+biases["b_out"])
  33.  
  34.  
  35. return output
  36.  
  37.  
  38. #GRAF
  39.  
  40. x = tf.placeholder('float',[None,784])
  41. y = tf.placeholder('float')
  42.  
  43. predictions = neural_network_model(x)
  44. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=predictions,labels=y))
  45.  
  46. optimizer = tf.train.AdamOptimizer().minimize(cost)
  47.  
  48. saver = tf.train.Saver()
  49.  
  50. # WYWOŁANIE SESJI
  51.  
  52. hm_epochs =3
  53.  
  54. with tf.Session() as sess:
  55.  
  56. sess.run(tf.global_variables_initializer())
  57.  
  58. for epoch in range(hm_epochs):
  59.  
  60. epoch_loss =0
  61. for _ in range(int(mnist.train.num_examples/batch_size)):
  62. epoch_x,epoch_y = mnist.train.next_batch(batch_size)
  63. _, c = sess.run([optimizer,cost],feed_dict = {x:epoch_x,y:epoch_y})
  64. epoch_loss +=c
  65. print("Epoch",epoch,'completed out of',hm_epochs,'loss:', epoch_loss)
  66.  
  67.  
  68.  
  69. correct = tf.equal(tf.argmax(predictions,1), tf.argmax(y,1))
  70. accuracy = tf.reduce_mean(tf.cast(correct,'float'))
  71. print("Accuracy:",accuracy.eval({x:mnist.train.images, y:mnist.train.labels}))
  72.  
  73.  
  74. save_path = saver.save(sess, "/tmp/model.ckpt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement