zoukankan      html  css  js  c++  java
  • mnist数据的预测结果以及批量处理

    import sys, os
    
    sys.path.append('F:mlDLsource-code')
    
    from dataset.mnist import load_mnist
    
    from PIL import Image
    
    import numpy as np
    
    #pickle提供了一个简单的持久化功能。可以将对象以文件的形式存放在磁盘上。
    #pickle模块只能在python中使用,python中几乎所有的数据类型(列表,字典,集合,类等)都可以用pickle来序列化,
    #pickle序列化后的数据,可读性差,人一般无法识别。
    import pickle
    
    def sigmoid(x):
        
        return 1 / (1 + np.exp(-x))
    
    def softmax(x):
        m = np.max(x)
        
        return np.exp(x- m) / np.sum(np.exp(x - m))
    
    def get_data():
        (x_train, t_train), (x_test, t_test) = load_mnist(normalize = True, flatten = True, one_hot_label = False)
        
        return x_test, t_test
    
    def init_network():
        with open("F:\mlDL\source-code\ch03\sample_weight.pkl", 'rb') as f:
            network = pickle.load(f)
            
        return network
    
    
    def predict(network, x):
        W1, W2, W3 = network['W1'], network['W2'], network['W3']
        
        b1, b2, b3 = network['b1'], network['b2'], network['b3']
        
        a1 = np.dot(x, W1) + b1
        
        z1 = sigmoid(a1)
        
        a2 = np.dot(a1, W2) + b2
        
        z2 = sigmoid(a2)
        
        a3 = np.dot(z2, W3) + b3
        
        y = softmax(a3)
        
        return y
    
    x, t = get_data()
    
    network = init_network()
    
    accuracy_cnt = 0
    
    for i in range(len(x)):
        y = predict(network, x[i])
        
        p = np.argmax(y)
        
        if p == t[i]:
            accuracy_cnt += 1
            
    print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
    Accuracy:0.8453


    #批处理显示
    x, t = get_data()
    
    network = init_network()
    
    batch_size = 100
    accuracy_cnt = 0
    
    for i in range(0, len(x), batch_size):
        x_batch = x[i:i+batch_size]
        
        y_batch = predict(network, x_batch)
        
        p = np.argmax(y_batch, axis = 1)
        
        accuracy_cnt += np.sum(p == t[i : i+batch_size])
        
    print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
    Accuracy:0.8453
     
  • 相关阅读:
    windows 系列机器查看tcp 配置
    test windows live writer
    win7 mysql 启动 问题
    用enum枚举量做下标初始化数组
    二层VXLAN静态配置
    pip install scrapy报错原文
    python练习2
    pycharm字体大小设置快捷键的方法
    python练习1
    centos 7下修改IP地址为静态
  • 原文地址:https://www.cnblogs.com/hyan0913/p/11529766.html
Copyright © 2011-2022 走看看