• tensorflow基础模型之KNN(最邻近值)算法


    KNN算法原理,本文将用tensorflow使用KNN算法训练MINST数据集。

    Codes:

    from __future__ import print_function, division

    import numpy as np
    import tensorflow as tf
    # 导入MNIST数据
    from tensorflow.examples.tutorials.mnist import input_data

    mnist = input_data.read_data_sets("./tmp/data/", one_hot=True)

    # 取5000训练数据和200测试数据
    Xtr, Ytr = mnist.train.next_batch(5000)  # 5000 for training (nn candidates)
    Xte, Yte = mnist.test.next_batch(200)  # 200 for testing

    # 图输入
    xtr = tf.placeholder("float", [None, 784])
    xte = tf.placeholder("float", [784])

    # 使用L1距离获取最邻近值
    # 计算L1距离
    distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1)
    # 预测:获取最小距离(最邻近值)
    pred = tf.arg_min(distance, 0)

    accuracy = 0.

    # 初始化参数
    init = tf.global_variables_initializer()

    # 开始训练
    with tf.Session() as sess:
       
       sess.run(init)

       # 测试
       for i in range(len(Xte)):
           # 获取最邻近
           nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})
           # 预测值与实际值对比
           print("Test", i, "Prediction:", np.argmax(Ytr[nn_index]),
                 "True Class:", np.argmax(Yte[i]))
           # 计算准确率
           if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
               accuracy += 1. / len(Xte)
       print("Done!")
       print("Accuracy:", accuracy)
    ---------------------

  • 相关阅读:
    浅析一类要求相邻不同的环上染色问题
    中国剩余定理(CRT)及其扩展(ExCRT)
    bsoj5988 [Achen模拟赛]期望 题解
    涂色游戏 题解
    [JZOJ A组]球 题解
    由 [SDOI2012]Longge的问题 探讨欧拉函数和莫比乌斯函数的一些性质和关联
    [NOIP模拟]文本编辑器 题解
    Nilearn 小记
    django 开发笔记1
    浅谈无需工作量证明的加密货币
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11182997.html
Copyright © 2020-2023  润新知