吴恩达机器学习作业TensorFlow实现(ex3,Mnist分类问题)

1、使用逻辑回归解决Mnist分类

# -*- coding: utf-8 -*-
"""
Created on Thu Jun 13 18:13:08 2019

@author: 无限未来
"""

import tensorflow as tf
import numpy as np
import scipy.io as scio
import random 
import matplotlib.pyplot as plt

# 读取数据
data_path='G:\DeepLearning\吴恩达作业\ex3data1.mat'
data = scio.loadmat(data_path)

X = data['X']
YY = data['y']

# 独热编码转换
Y = np.zeros([5000,10])
for i in range(5000):
    Y[i,YY[i]-1] = 1
    
pic_show = np.zeros([200,200])
content = np.zeros([20,20])
pic_choose = random.sample(range(5000), 100)
for i in range(10):
    for j in range(10):
        #order='F'列优先
        content = X[pic_choose[(i*10+j)],:].reshape(20,20,order='F')
        pic_show[i*20:(i*20+20),j*20:(j*20+20)] = content
    
plt.figure("example") # 
plt.rcParams['figure.dpi'] = 200 #分辨率
plt.rcParams['savefig.dpi'] = 200 #图片像素
plt.imshow(pic_show) # 二维数组的数据

# 训练模型
tf.reset_default_graph()
# tf Graph Input
x = tf.placeholder(tf.float32, [None, 400]) # mnist data维度 
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 数字=> 10 classes

# Set model weights
W = tf.Variable(tf.random_normal([400, 10]))
b = tf.Variable(tf.zeros([10]))

# 构建模型
pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax分类

# Minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))
#参数设置
learning_rate = 0.2
# 使用梯度下降优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# 训练样本集250次,训练批量为100,每训练10次展示一次
training_epochs = 250
batch_size = 100
display_step = 10


# 启动session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())# Initializing OP

    # 启动循环开始训练
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(5000/batch_size)
        # 遍历全部数据集
        for i in range(total_batch):
            batch_xs = X[i*batch_size:(i+1)*batch_size,:]
            batch_ys = Y[i*batch_size:(i+1)*batch_size,:]
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,
                                                          y: batch_ys})
            # Compute average loss
            avg_cost += c / total_batch
        # 显示训练中的详细信息
        if (epoch+1) % display_step == 0:
            print ("次数:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))

    print( "完成!")

    # 测试 model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    # 计算准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print ("准确率:", accuracy.eval({x: X, y: Y}))

2、采用神经网络解决(单隐藏层,25节点)

# -*- coding: utf-8 -*-
"""
Created on Thu Jun 13 22:15:01 2019

@author: 无限未来
"""

import tensorflow as tf
import numpy as np
import scipy.io as scio
import random 
import matplotlib.pyplot as plt

# 读取数据
data_path='G:\DeepLearning\吴恩达作业\ex3data1.mat'
data = scio.loadmat(data_path)

X = data['X']
YY = data['y']

Y = np.zeros([5000,10])
for i in range(5000):
    Y[i,YY[i]-1] = 1
    
pic_show = np.zeros([200,200])
content = np.zeros([20,20])
pic_choose = random.sample(range(5000), 100)
for i in range(10):
    for j in range(10):
        #order='F'列优先
        content = X[pic_choose[(i*10+j)],:].reshape(20,20,order='F')
        pic_show[i*20:(i*20+20),j*20:(j*20+20)] = content
    
plt.figure("example") # 
plt.rcParams['figure.dpi'] = 200 #分辨率
plt.rcParams['savefig.dpi'] = 200 #图片像素
plt.imshow(pic_show) # 二维数组的数据

# 训练模型
tf.reset_default_graph()

#参数设置
learning_rate = 0.01
training_epochs = 250
batch_size = 100
display_step = 10

# Network Parameters
n_hidden = 25 
n_input = 400 # MNIST data 输入 (img shape: 28*28)
n_classes = 10  # MNIST 列别 (0-9 ,一共10类)

# tf Graph Input
x = tf.placeholder(tf.float32, [None, n_input]) # mnist data维度 
y = tf.placeholder(tf.float32, [None, n_classes]) # 0-9 数字=> 10 classes

# Create model
def multilayer_perceptron(x, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h']), biases['b'])
    layer_1 = tf.nn.sigmoid(layer_1)

    # Output layer with linear activation
    out_layer = tf.matmul(layer_1, weights['out']) + biases['out']
    return out_layer
    
# Store layers weight & bias
weights = {
    'h': tf.Variable(tf.random_normal([n_input, n_hidden])),
    'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
    'b': tf.Variable(tf.random_normal([n_hidden])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

# 构建模型
pred = multilayer_perceptron(x, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# 初始化变量
init = tf.global_variables_initializer()


# 启动session
with tf.Session() as sess:
    sess.run(init)

    # 启动循环开始训练
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(5000/batch_size)
        # 遍历全部数据集
        for i in range(total_batch):
            batch_xs = X[i*batch_size:(i+1)*batch_size,:]
            batch_ys = Y[i*batch_size:(i+1)*batch_size,:]
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,
                                                          y: batch_ys})
            # Compute average loss
            avg_cost += c / total_batch
        # 显示训练中的详细信息
        if (epoch+1) % display_step == 0:
            print ("Epoch:", '%04d' % (epoch+1), "cost=", \
                "{:.9f}".format(avg_cost))
    print (" Finished!")

    # 测试 model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    # 计算准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print ("准确率:", accuracy.eval({x: X, y: Y}))

 

你可能感兴趣的:(机器学习)