tensorflow实现两层神经网络 (附代码)

这里有两份代码,第一份代码是自己写的,第二份来自于腾讯云。腾讯云的代码可以达到90%的准确率。

 

import tensorflow as tf
import numpy as np
import pickle
import gzip
# 首先定义输入

def get_input():
    f = gzip.open('./mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = pickle.load(f,encoding='bytes')
    train_x = [np.reshape(x,(1,784)) for x in training_data[0]]
    train_y = [vectorized(y) for y in training_data[1]]
    test_x = [np.reshape(x,(1,784)) for x in test_data[0]]
    test_y = [vectorized(y) for y in test_data[1]]
    return train_x,train_y,test_x,test_y

def get_batch(train_x,train_y,batch_size):
    a = np.arange(len(train_x))
    np.random.shuffle(a)
    t_x = np.reshape(train_x,(len(train_x),784))
    t_y = np.reshape(train_y,(len(train_y),10))
    t_xi = [t_x[a[i]] for i in range(batch_size)]
    t_yi = [t_y[a[i]] for i in range(batch_size)]
    return t_xi,t_yi

def vectorized(num):
    e = np.zeros((10, 1))
    e[num] = 1
    return e

def predict(test_x,test_y,X,Y,batch_size):
    # 下面要预测,计算准确率,
    data_num = len(test_x)
    print("this is test data_num:",data_num)
    count=0
    print("this is predict")
    for data_index in range(0,data_num,batch_size):
        t_x = np.reshape(test_x[data_index:data_index+batch_size],(batch_size,784))
        # t_x = t_x.T
        t_y = np.reshape(test_y[data_index:data_index+batch_size],(batch_size,10))
        
        a = sess.run(Y,feed_dict = {X:t_x})
        # print(a.shape)
        for i in range(len(a)):
            if return_big(t_y[i]) == return_big(a[i]):
                count+=1
    print("this is count:",count)
    print("this is precise:",count/data_num)

def return_big(arr):
    # 这里实现相同作用的函数是tf.argmax
    # 用于返回一维数组中最大值的索引
    t = arr[0]
    t_flag = 0
    for i in range(1,len(arr)):
        if arr[i]> t:
            t = arr[i]
            t_flag = i
    return t_flag

if __name__ == "__main__":
    batch_size = 100
    # 隐藏层神经元的数目
    hidden_layer1 = 500
    hidden_layer2 = 500
    #学习率
    learning_rate = 0.1
    #迭代的次数
    epoch_num = 150
    x = tf.placeholder(tf.float32,[batch_size,784])
    y_pred = tf.placeholder(tf.float32,[batch_size,10])
    # y_pred用来装分类的结果,即labels

    w1 = tf.Variable(tf.truncated_normal([784,hidden_layer1],stddev = 0.1))
    # w1 = tf.Variable(tf.random_normal([784,hidden_layer1]))
    b1 = tf.Variable(tf.zeros([1,hidden_layer1])+0.01)
    w2 = tf.Variable(tf.truncated_normal([hidden_layer1,hidden_layer2],stddev = 0.1))
    # w2 = tf.Variable(tf.random_normal([hidden_layer1,hidden_layer2]))
    b2 = tf.Variable(tf.zeros([1,hidden_layer2])+0.01)
    w3 = tf.Variable(tf.truncated_normal([hidden_layer2,10],stddev = 0.1))
    # w3 = tf.Variable(tf.random_normal([hidden_layer2,10]))
    b3 = tf.Variable(tf.zeros([1,10])+0.01)

    h1 = tf.nn.relu(tf.matmul(x,w1)+b1)
    h2 = tf.nn.relu(tf.matmul(h1,w2)+b2)
    y = tf.nn.softmax(tf.matmul(h2,w3)+b3)

    #上面是前向传播的过程,下面是反向传播
    entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_pred, logits=y)
    loss = tf.reduce_mean(entropy)
    optimize = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)
        train_x,train_y,test_x,test_y = get_input()
        print(len(train_x))
        print(len(train_y))
        for i in range(epoch_num):
            t_x,t_y = get_batch(train_x,train_y,batch_size)
            _,_loss = sess.run([optimize,loss],feed_dict = {x:t_x,y_pred:t_y})
            print("loss of epoches[{0}]:{1}".format(i,_loss))

        # 下面是计算预测准确率
        predict(test_x,test_y,x,y,batch_size)

下面是腾讯云的,但是是我改动过了的,只改动了一些参数,让它的参数跟我第一份代码的一致,好做对比。

#-*- encoding:utf-8 -*-
#!/usr/local/env python

import numpy as np
import tensorflow as tf
import pickle
import gzip
# from tensorflow.examples.tutorials.mnist import input_data

def add_layer(inputs, in_size, out_size, activation_function=None):
    W = tf.Variable(tf.random_normal([in_size, out_size]))
    b = tf.Variable(tf.zeros([1, out_size]) + 0.01)

    Z = tf.matmul(inputs, W) + b
    if activation_function is None:
        outputs = Z
    else:
        outputs = activation_function(Z)

    return outputs

def get_batch(train_x,train_y,batch_size):
    a = np.arange(len(train_x))
    np.random.shuffle(a)
    t_x = np.reshape(train_x,(len(train_x),784))
    t_y = np.reshape(train_y,(len(train_y),10))
    t_xi = [t_x[a[i]] for i in range(batch_size)]
    t_yi = [t_y[a[i]] for i in range(batch_size)]
    return t_xi,t_yi

def get_input():
    f = gzip.open('./mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = pickle.load(f,encoding='bytes')
    train_x = [np.reshape(x,(784,1)) for x in training_data[0]]
    train_y = [vectorized(y) for y in training_data[1]]
    test_x = [np.reshape(x,(784,1)) for x in test_data[0]]
    test_y = [vectorized(y) for y in test_data[1]]
    return train_x,train_y,test_x,test_y

def vectorized(num):
    e = np.zeros((10, 1))
    e[num] = 1
    return e

def return_big(arr):
    # 这里实现相同作用的函数是tf.argmax
    # 用于返回一维数组中最大值的索引
    t = arr[0]
    t_flag = 0
    for i in range(1,len(arr)):
        if arr[i]> t:
            t = arr[i]
            t_flag = i
    return t_flag

def predict(test_x,test_y,X,Y,batch_size):
    # 下面要预测,计算准确率,
    data_num = len(test_x)
    print("this is test data_num:",data_num)
    count=0
    print("this is predict")
    for data_index in range(0,data_num,batch_size):
        t_x = np.reshape(test_x[data_index:data_index+batch_size],(batch_size,784))
        # t_x = t_x.T
        t_y = np.reshape(test_y[data_index:data_index+batch_size],(batch_size,10))
        
        a = sess.run(Y,feed_dict = {X:t_x})
        # print(a.shape)
        for i in range(len(a)):
            if return_big(t_y[i]) == return_big(a[i]):
                count+=1
    print("this is count:",count)
    print("this is precise:",count/data_num)

if __name__ == "__main__":

    # MNIST = input_data.read_data_sets("mnist", one_hot=True)
    # print(MNIST)
    learning_rate = 0.01
    batch_size = 100
    n_epochs = 100

    X = tf.placeholder(tf.float32, [batch_size, 784])
    Y = tf.placeholder(tf.float32, [batch_size, 10])

    layer_dims = [784, 500, 500, 10]
    layer_count = len(layer_dims)-1 # 不算输入层
    layer_iter = X

    for l in range(1, layer_count): # layer [1,layer_count-1] is hidden layer
        layer_iter = add_layer(layer_iter, layer_dims[l-1], layer_dims[l], activation_function=tf.nn.relu)
    prediction = add_layer(layer_iter, layer_dims[layer_count-1], layer_dims[layer_count], activation_function=None)

    entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=prediction)
    loss = tf.reduce_mean(entropy)

    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)

        # n_batches = int(MNIST.test.num_examples/batch_size)
        # print("this is n_batches:",n_batches)
        # print("this is n_epochs:",n_epochs)
        train_x,train_y,test_x,test_y = get_input()
        print("this is train_x len:",len(train_x))
        for i in range(n_epochs):
            print("this is ",i," epochs!!")
            # for j in range(n_batches):
                # X_batch, Y_batch = MNIST.train.next_batch(batch_size)
            X_batch, Y_batch = get_batch(train_x,train_y,batch_size)
                # print("this is x_batch shape:",len(X_batch))
                # print("this is X_batch shape:",X_batch.shape)
                # print("this is Y_batch shape:",Y_batch.shape)
            _, loss_ = sess.run([optimizer, loss], feed_dict={X: X_batch, Y: Y_batch})
                # if i % 10 == 5 and j == 0:
            print("Loss of epochs[{0}]: {1}".format(i, loss_))

        # test the model
        # n_batches = int(MNIST.test.num_examples/batch_size)
        # total_correct_preds = 0
        # for i in range(n_batches):
        # X_batch, Y_batch = MNIST.test.next_batch(10000)
        # predict(X_batch,Y_batch,X,prediction,batch_size)
        predict(test_x,test_y,X,prediction,batch_size)
            # preds = sess.run(prediction, feed_dict={X: X_batch, Y: Y_batch})
            # correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y_batch, 1))
            # accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32)) 

            # total_correct_preds += sess.run(accuracy)

        # print("Accuracy {0}".format(total_correct_preds/MNIST.test.num_examples))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值