莫烦Python--Tensorflow Day3

Classification 分类学习

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# number 1 to 10 data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

def add_layer(inputs, in_size, out_size, activation_function=None):
    # add one more layer and return the output of this layer
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

# define compute_accuracy
def compute_accuracy(v_xs, v_ys):
    global prediction
    y_pre = sess.run(prediction, feed_dict={xs: v_xs})                      # 生成预测值(0和1之间的概率)
    correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))  # 对比预测值是否和真实值中1这个位置的最大值一致,1代表每一行最大值的索引,是搜出来的
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))      # 计算datasets中有多少个对的和多少个错的,tf.cast的作用是转换数据类型的
    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
    return result

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784])                                # 28 * 28输入像素点
ys = tf.placeholder(tf.float32, [None, 10])                                 # 输出

# add output layer
prediction = add_layer(xs, 784, 10, activation_function=tf.nn.softmax)      # softmax is used to classificate

# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
                                              reduction_indices=[1]))       # loss, 交叉熵损失函数
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

# 激活会话设置

sess = tf.Session()

# the most important step , activate the initialize
sess.run(tf.initialize_all_variables())

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)                        # 批处理,mini batch的处理结果不见得比一次性全处理的效果差
    sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})
    if i % 50 == 0:
        print(compute_accuracy(
            mnist.test.images, mnist.test.labels))

'''
运行结果:
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
0.0817 
0.6302
0.7421
0.7901
0.8085
0.8241
0.8341
0.8405
0.8481
0.8544
0.8539
0.8595
0.8612
0.8653
0.8656
0.867
0.8695
0.8739
0.8783
0.8761
'''

怎么解决过拟合问题?

'''过拟合(自负)'''
'''
怎么解决过拟合问题?
1、增加数据量
2、regularization,将不同度量的数据映射到相同值域的空间内
   分为:L1,L2...
   y = Wx
   L1(LASSO): cost = (Wx - real y)^2 + abs(W)
   L2(Ridge Regression): cost = (Wx - real y)^2 + (W)^2
   L3,L4...
3、Dropout regularization(岭估计)
   每次训练的时候让神经网络随机丢掉一些神经元和连接让它进行训练,让它不依赖于某一特定的神经元,Dropout让神经网络没有办法过度依赖特定的神经网络。
   regularization就会过度依赖W,导致L1,L2会惩罚大的W。
'''

dropout 解决 overfitting

你可能感兴趣的:(笔记)