TensorFlow是什么?
Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow为张量从流图的一端流动到另一端计算过程,是将复杂的数据结构传输至神经网络中进行分析和处理的系统。
TensorFlow能干什么?
图像识别,语音识别等
什么是欠拟合,什么是过拟合?
为什么要正则化?
降低模型的复杂度以减少过拟合
如何正则化?
dropout:删除为零的神经元
为什么要将数据拆分为训练集合测试集?
用于判断给定模型能否很好地泛化到新数据
什么是代价函数?
个人理解: 衡量预测值和真实值差距的函数
常用的代价函数有哪些?
二次代价函数
tf.reduce_mean(tf.square(真实值 - 预测值))
交叉熵
tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
labels=真实值,
logits=预测值))
激活函数作用?
对非线性问题进行建模
import tensorflow as tf;
import numpy as np;
import matplotlib.pyplot as plt;
x = np.linspace(-10, 10, 100)
y0 = tf.nn.softmax(x)
y1 = tf.nn.sigmoid(x)
y2 = tf.nn.tanh(x)
y3 = tf.nn.elu(x)
y4 = tf.nn.softplus(x)
y5 = tf.nn.softsign(x)
y6 = tf.nn.relu(x)
y7 = tf.nn.relu6(x)
with tf.Session() as sess:
ax0 = plt.subplot2grid((4, 2), (0, 0))
ax0.plot(x, sess.run(y0))
ax0.set_title('softmax')
ax1 = plt.subplot2grid((4, 2), (0, 1))
ax1.plot(x, sess.run(y1))
ax1.set_title('sigmoid')
ax2 = plt.subplot2grid((4, 2), (1, 0))
ax2.plot(x, sess.run(y2))
ax2.set_title('tanh')
ax3 = plt.subplot2grid((4, 2), (1, 1))
ax3.plot(x, sess.run(y3))
ax3.set_title('elu')
ax4 = plt.subplot2grid((4, 2), (2, 0))
ax4.plot(x, sess.run(y4))
ax4.set_title('softplus')
ax5 = plt.subplot2grid((4, 2), (2, 1))
ax5.plot(x, sess.run(y5))
ax5.set_title('softsign')
ax6 = plt.subplot2grid((4, 2), (3, 0))
ax6.plot(x, sess.run(y6))
ax6.set_title('relu')
ax7 = plt.subplot2grid((4, 2), (3, 1))
ax7.plot(x, sess.run(y7))
ax7.set_title('relu6')
plt.show()
常用的优化器有哪些?
梯度下降算法
tf.train.GradientDescentOptimizer(
learning_rate,
use_locking=False,
name='GradientDescent')
动量梯度下降算法
tf.train.MomentumOptimizer(
learning_rate,
momentum,
use_locking=False,
name='Momentum',
use_nesterov=False)
Adam优化算法
tf.train.AdamOptimizer(
learning_rate=0.001,
beta1=0.9,
beta2=0.999,
epsilon=1e-08,
use_locking=False,
name='Adam')
手写数字识别
import time
import pandas as pd
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 载入数据s
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
# 每批次大小
batch_size = 100
# 计算一共有多少批次
n_batch = mnist.train.num_examples
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
# 创建一个简单的神经网络 经常使用0
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x, W) + b)
# 二次代价函数
# loss = tf.reduce_mean(tf.square(y - prediction))
# 交叉熵代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
# 使用梯度下降算法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
# 返回一个布尔值列表
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
start = time.clock()
sess.run(tf.global_variables_initializer())
for epoch in range(21):
for batch in range(500): # n_batch 55000太大了
batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 获取n个train中的数据
sess.run(train_step, feed_dict={x:batch_xs, y:batch_ys})
acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y: mnist.test.labels})
print(str(epoch) + ': ' + str(acc))
end = time.clock()
print(end - start)
不同优化器训练结果
GradientDescentOptimizer(0.2)
0: 0.8235
1: 0.8833
2: 0.899
3: 0.9047
4: 0.9066
5: 0.91
6: 0.9107
7: 0.9125
8: 0.9147
9: 0.9156
10: 0.9162
11: 0.917
12: 0.917
13: 0.9184
14: 0.9187
15: 0.9189
16: 0.92
17: 0.9201
18: 0.9208
19: 0.9214
20: 0.92
18.959907000000015
MomentumOptimizer(0.2, 0.5)
0: 0.8896
1: 0.9045
2: 0.9079
3: 0.9122
4: 0.914
5: 0.9171
6: 0.9182
7: 0.9195
8: 0.9215
9: 0.9199
10: 0.921
11: 0.9226
12: 0.9211
13: 0.9231
14: 0.9229
15: 0.9231
16: 0.9239
17: 0.9254
18: 0.9233
19: 0.925
20: 0.9249
19.230733000000015
AdamOptimizer(0.2)
0: 0.8047
1: 0.8163
2: 0.8745
3: 0.8782
4: 0.8981
5: 0.9001
6: 0.8927
7: 0.8943
8: 0.8764
9: 0.8962
10: 0.9084
11: 0.8991
12: 0.9022
13: 0.9021
14: 0.9019
15: 0.9078
16: 0.9138
17: 0.9129
18: 0.889
19: 0.9064
20: 0.9069
19.567082
参考
官网
tensorflow系列
TensorFlow三种常用的优化器