阅读背景:

tensorflow(4)——逻辑回归

来源:互联网 

学习《Tensorflow入门教程》记录 

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

mnist = input_data.read_data_sets('data/', one_hot=True)  #读取数据集

#设置参数
numClasses = 10  #输出类别数
inputSize = 784  #输入数据大小28*28*1
trainingIterations = 50000  #迭代次数
batchSize = 64  #一次迭代多少个

#指定X和y的大小
X = tf.placeholder(tf.float32, shape = [None, inputSize])
y = tf.placeholder(tf.float32, shape = [None, numClasses])

#参数初始化
W1 = tf.Variable(tf.random_normal([inputSize, numClasses], stddev=0.1))  #随机取出服从正态分布的784*10个数
B1 = tf.Variable(tf.constant(0.1), [numClasses])  #得到一个10维的常量0.1

#构造模型
y_pred = tf.nn.softmax(tf.matmul(X, W1) + B1)   #把构造的函数传入softmax分类器中

loss = tf.reduce_mean(tf.square(y - y_pred))   # 以预估值y_pred和实际值y之间的均方误差作为损失
opt = tf.train.GradientDescentOptimizer(learning_rate = .05).minimize(loss)  # 采用梯度下降法来优化参数

correct_prediction = tf.equal(tf.argmax(y_pred,1), tf.argmax(y,1))  #预测值和真实值是否相等
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))  #计算精度

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

for i in range(trainingIterations):
    batch = mnist.train.next_batch(batchSize)
    batchInput = batch[0]
    batchLabels = batch[1]
    _, trainingLoss = sess.run([opt, loss], feed_dict={X: batchInput, y: batchLabels})
    if i%1000 == 0:
        train_accuracy = accuracy.eval(session=sess, feed_dict={X: batchInput, y: batchLabels})
        print ("step %d, training accuracy %g"%(i, train_accuracy))from tensorflow.



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: