Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tensorflow as tf
- sess = tf.InteractiveSession()
- import numpy as np
- from numpy import genfromtxt
- def weight_variable(shape):
- initial = tf.truncated_normal(shape, stddev=0.1)
- return tf.Variable(initial)
- def bias_variable(shape):
- initial = tf.constant(0.1, shape=shape)
- return tf.Variable(initial)
- def conv2d(x, W):
- return tf.nn.conv2d(x, W, strides=[1, 1, 3*3, 1], padding='VALID')
- # strides = [batch , height , width , channel]
- data = genfromtxt('circle_deeplearn_data_small.txt',delimiter=',')
- out = genfromtxt('circle_deeplearn_output_small.txt',delimiter=',')
- x = tf.placeholder(tf.float32, shape =[None, 3*3*15]) # size of x
- y_ = tf.placeholder(tf.float32, shape =[None, 1]) # size of output
- W_conv1 = weight_variable([1,3*3,1,15])
- b_conv1 = bias_variable([15])
- x_image = tf.reshape(x,[-1,1,3*3*15,1])
- h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)
- W_fc1 = weight_variable([1 * 1 * 15 , 1])
- b_fc1 = bias_variable([1])
- h_conv1_flat = tf.reshape(h_conv1 , [-1,1 * 1 * 15])
- h_fc1 = tf.nn.relu(tf.matmul(h_conv1_flat , W_fc1) + b_fc1)
- y_conv = h_fc1
- keep_prob = tf.placeholder(tf.float32)
- h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
- #Adam
- cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
- train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
- correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
- #sess.run(tf.global_variables_initializer())
- sess.run(tf.initialize_all_variables())
- for i in range(20000):
- batch = data.train.next_batch(50)
- if i%100 == 0:
- train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
- print("step %d, training accuracy %g"%(i, train_accuracy))
- train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
- 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