• wenbo123
    了解作者
  • Python
    开发工具
  • 1KB
    文件大小
  • gz
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2018-09-30 21:52
    上传日期
在linux系统用tensorflow实现perceptron
perceptron.py.tar.gz
  • perceptron.py
    4.3KB
内容介绍
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data input_units = 784 hidden1_units = 300 output_units = 10 sess = tf.InteractiveSession() def main(): # Import data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # Create the model x = tf.placeholder(tf.float32, [None, input_units]) y_ = tf.placeholder(tf.float32, [None, output_units]) keep_prob = tf.placeholder(tf.float32) #hidden1 W1 = tf.Variable(tf.truncated_normal([input_units, hidden1_units], stddev=0.1)) b1 = tf.Variable(tf.zeros([hidden1_units])) hidden1 = tf.nn.relu(tf.matmul(x, W1) + b1) hidden1_drop = tf.nn.dropout(hidden1,keep_prob) # output W2 = tf.Variable(tf.truncated_normal([hidden1_units, output_units], stddev=0.1)) b2 = tf.Variable(tf.zeros([output_units])) y = tf.nn.softmax(tf.matmul(hidden1_drop, W2) + b2) # Define loss and optimizer cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy) tf.global_variables_initializer().run() # Train for i in range(3000): batch_x, batch_y = mnist.train.next_batch(100) train_step.run(feed_dict={x: batch_x, y_: batch_y, keep_prob: 0.75}) # Test trained model correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) if __name__ == '__main__': main() import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data input_units = 784 hidden1_units = 300 output_units = 10 sess = tf.InteractiveSession() def weight_variable(shape): return tf.Variable(tf.truncated_normal(shape=shape, stddev=0.1)) def bias_variable(shape): return tf.Variable(tf.constant(0.1, shape=shape)) def conv_relu(x, W, b, strides_size): return tf.nn.relu(tf.nn.conv2d(x, W, strides=[1, strides_size, strides_size, 1], padding='SAME') + b) def maxpool_process(x, kernel_size, strides_size): return tf.nn.max_pool(x, ksize=[1, kernel_size, kernel_size, 1], strides=[1, strides_size, strides_size, 1], padding='SAME') if __name__ == '__main__': # Import data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # Create the model x = tf.placeholder(tf.float32, [None, input_units]) y_ = tf.placeholder(tf.float32, [None, output_units]) x_image = tf.reshape(x, [-1, 28, 28, 1]) keep_prob = tf.placeholder(tf.float32) # hidden1 W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) hidden_conv1 = conv_relu(x_image, W_conv1, b_conv1, 1) hidden_conv1_maxpool = maxpool_process(hidden_conv1, 2, 2) # hidden2 W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) hidden_conv2 = conv_relu(hidden_conv1_maxpool, W_conv2, b_conv2, 1) hidden_conv2_maxpool = maxpool_process(hidden_conv2, 2, 2) # Full connection W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) hidden_fc1_flat = tf.reshape(hidden_conv2_maxpool, [-1, 7 * 7 * 64]) hidden_fc1 = tf.nn.relu(tf.matmul(hidden_fc1_flat, W_fc1) + b_fc1) hidden_fc1_drop = tf.nn.dropout(hidden_fc1, keep_prob) # output W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y = tf.nn.softmax(tf.matmul(hidden_fc1_drop, W_fc2) + b_fc2) # Define loss and optimizer cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.global_variables_initializer().run() # Train for i in range(20000): batch = mnist.train.next_batch(50) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) # Test trained model if i % 100 == 0: train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}) print("step %d \t test accuracy %g" % (i, train_accuracy)) print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
评论
    相关推荐