逻辑回归(logistic regression)


In [1]:
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time

1.读取数据


In [2]:
mnist = input_data.read_data_sets('data/mnist', one_hot=True)


Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting data/mnist/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting data/mnist/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting data/mnist/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/mnist/t10k-labels-idx1-ubyte.gz

2.构建图(Graph)


In [12]:
# 参数
lr = .001
batch_size = 128
n_epochs = 30

X = tf.placeholder(tf.float32, [None, 784], name='X')
Y = tf.placeholder(tf.int32, [None, 10], name='Y')
w = tf.Variable(tf.random_normal([784, 10], stddev=.01), name='weights')
b = tf.Variable(tf.zeros([10]), name='bias')

logits = tf.matmul(X, w) + b

entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y, name='loss')
loss = tf.reduce_mean(entropy)

optimizer = tf.train.AdadeltaOptimizer(lr).minimize(loss)

3.在 Session 中运行图(Graph)


In [ ]:

4.绘制结果


In [ ]: