Guest User

Untitled

a guest
Feb 21st, 2017
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. import tensorflow as tf
  2. sess = tf.InteractiveSession()
  3.  
  4. import numpy as np
  5. from numpy import genfromtxt
  6.  
  7. def weight_variable(shape):
  8.     initial = tf.truncated_normal(shape, stddev=0.1)
  9.     return tf.Variable(initial)
  10.  
  11. def bias_variable(shape):
  12.     initial = tf.constant(0.1, shape=shape)
  13.     return tf.Variable(initial)
  14.  
  15. def conv2d(x, W):
  16.     return tf.nn.conv2d(x, W, strides=[1, 1, 3*3, 1], padding='VALID')
  17.   # strides = [batch , height , width , channel]
  18.  
  19. data = genfromtxt('circle_deeplearn_data_small.txt',delimiter=',')
  20. out = genfromtxt('circle_deeplearn_output_small.txt',delimiter=',')
  21.  
  22. x = tf.placeholder(tf.float32, shape =[None, 3*3*15]) # size of x
  23. y_ = tf.placeholder(tf.float32, shape =[None, 1])   # size of output
  24.  
  25.  
  26.  
  27.  
  28. W_conv1 = weight_variable([1,3*3,1,15])
  29. b_conv1 = bias_variable([15])
  30.  
  31. x_image = tf.reshape(x,[-1,1,3*3*15,1])
  32.  
  33. h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)
  34.  
  35.  
  36. W_fc1 = weight_variable([1 * 1 * 15 , 1])
  37. b_fc1 = bias_variable([1])
  38.  
  39. h_conv1_flat = tf.reshape(h_conv1 , [-1,1 * 1 * 15])
  40. h_fc1 = tf.nn.relu(tf.matmul(h_conv1_flat , W_fc1) + b_fc1)
  41. y_conv = h_fc1
  42. keep_prob = tf.placeholder(tf.float32)
  43. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  44.  
  45.  
  46.  
  47. #Adam
  48.  
  49. cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
  50. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  51. correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
  52. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  53. #sess.run(tf.global_variables_initializer())   
  54. sess.run(tf.initialize_all_variables())
  55. for i in range(20000):
  56.     batch = data.train.next_batch(50)
  57.     if i%100 == 0:
  58.         train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
  59.         print("step %d, training accuracy %g"%(i, train_accuracy))
  60.     train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
  61. print("test accuracy %g"%accuracy.eval(feed_dict={x: data, y_: out, keep_prob: 1.0}))
Advertisement
Add Comment
Please, Sign In to add comment