码迷,mamicode.com
首页 > 其他好文 > 详细

由浅入深之Tensorflow(2)----logic_regression实现

时间:2016-12-06 09:20:18      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:std   var   set   nbsp   data   weight   --   ges   git   

 

 

 

import tensorflow as tf
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data

def initWeights(shape):
    return tf.Variable(tf.random_normal(shape, stddev = 0.1))

def initBiases(shape):
    return tf.Variable(tf.random_normal(shape, stddev = 0.1))

def model(X, weights, baises):
    return tf.matmul(X, weights) + baises

mnist = input_data.read_data_sets(MNIST_data/, one_hot = True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

X = tf.placeholder(float, [None, 784])
Y = tf.placeholder(float, [None, 10])

learning_rate = 0.05
epcoh = 100

weights = initWeights([784,10])
biases = initBiases([10])

y_ = model(X, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_, Y))
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
predict_op = tf.argmax(y_, 1)

with tf.Session() as sess:
    tf.initialize_all_variables().run()
    for i in range(epcoh):
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
            sess.run(train_op, feed_dict = {X: trX[start:end], Y: trY[start:end]})
        print (i, np.mean(np.argmax(teY, axis=1) == sess.run(predict_op, feed_dict={X: teX})))

 

由浅入深之Tensorflow(2)----logic_regression实现

标签:std   var   set   nbsp   data   weight   --   ges   git   

原文地址:http://www.cnblogs.com/upright/p/6136199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!