博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TensorFlow Classification 分类学习
阅读量:5214 次
发布时间:2019-06-14

本文共 2398 字,大约阅读时间需要 7 分钟。

# View more python learning tutorial on my Youtube and Youku channel!!!# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg# Youku video tutorial: http://i.youku.com/pythontutorial"""Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly."""from __future__ import print_functionimport tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data# number 1 to 10 datamnist = 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 outputsdef compute_accuracy(v_xs, v_ys):    global prediction    y_pre = sess.run(prediction, feed_dict={xs: v_xs})    correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})    return result# define placeholder for inputs to networkxs = tf.placeholder(tf.float32, [None, 784]) # 28x28ys = tf.placeholder(tf.float32, [None, 10])# add output layerprediction = add_layer(xs, 784, 10,  activation_function=tf.nn.softmax)# the error between prediction and real datacross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),                                              reduction_indices=[1]))       # losstrain_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)sess = tf.Session()# important step# tf.initialize_all_variables() no long valid from# 2017-03-02 if using tensorflow >= 0.12if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:    init = tf.initialize_all_variables()else:    init = tf.global_variables_initializer()sess.run(init)for i in range(1000):    batch_xs, batch_ys = mnist.train.next_batch(100)    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))

 

转载于:https://www.cnblogs.com/francischeng/p/9693447.html

你可能感兴趣的文章
Windows10一周年庆典壁纸
查看>>
kibana对logstash监控获取不到数据
查看>>
UPC 2224 Boring Counting ★(山东省第四届ACM程序设计竞赛 tag:线段树)
查看>>
IIS7上设置MIME让其支持android和Iphone的更新下载
查看>>
数据库系统原理
查看>>
leetcode 947. Most Stones Removed with Same Row or Column
查看>>
mong 按 geometry 搜索 地理位置信息
查看>>
框架网址和其他的一些网址
查看>>
angular ng-class\tab切换(从服务器引入数据)或用指令写
查看>>
不要追求最新的技术
查看>>
Oracle Golden Gate 系列十五 -- GG Trails 说明
查看>>
Oracle 11g 新特性 -- SecureFiles 说明
查看>>
Spring Cloud Zuul 网关使用与 OAuth2.0 认证授权服务
查看>>
梳理 Opengl ES 3.0 (三)顶点坐标变换
查看>>
Office2010安装错误
查看>>
Selenium2+python自动化7-xpath定位
查看>>
算法导论笔记:02基本排序查找算法
查看>>
Redis源码解析:08对象
查看>>
AIDL--------应用之间的通信接口
查看>>
java的JVM机制
查看>>