1、Random
import tensorflow as tf
x = tf.random_uniform([10, ], minval=-1, maxval=1)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('x:', sess.run(x))
2、ReLU
import tensorflow as tf
x = [-5., 0., 5., 10.]
y = tf.nn.relu(x)
y2 = tf.nn.relu6(x)
with tf.Session() as sess:
print('x:', x)
print('ReLU:', sess.run(y))
print('ReLU6', sess.run(y2))
3、MINIST
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
y_actual = tf.placeholder(tf.float32, [None, 10])
# 初始化权值W
W = tf.Variable(tf.random_uniform([784, 500], -1., 1.))
W2 = tf.Variable(tf.random_uniform([500, 10], -1., 1.))
# 初始化偏置项b
b = tf.Variable(tf.zeros([500]))
b2 = tf.Variable(tf.zeros([10]))
# 加权变换,添加ReLU非线性激励函数
y_ = tf.nn.relu((tf.matmul(x, W) + b))
output = tf.matmul(y_, W2) + b2
# 求交叉熵
loss = tf.losses.softmax_cross_entropy(onehot_labels=y_actual, logits=output)
# 用梯度下降法使得残差最小
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
# 在测试阶段,测试准确度计算
correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(y_actual, 1))
# 多个批次的准确度均值
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for i in range(100000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys})
if i % 100 == 0:
print("test_accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images, y_actual: mnist.test.labels}))
4、MINIST_API
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
y_actual = tf.placeholder(tf.float32, [None, 10])
y_ = tf.layers.dense(x, 500, activation='relu')
output = tf.layers.dense(y_, 10)
# 求交叉熵
loss = tf.losses.softmax_cross_entropy(onehot_labels=y_actual, logits=output)
# 用梯度下降法使得残差最小
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
# 在测试阶段,测试准确度计算
correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(y_actual, 1))
# 多个批次的准确度均值
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for i in range(100000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys})
if i % 100 == 0:
print("test_accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images, y_actual: mnist.test.labels}))