Logistic回归,处理MNIST,举例。


Logistic回归,处理MNIST,举例,请参考。

# coding = utf_8
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'
import tensorflow as tf
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev = 0.01))


def model(X, w):
    return tf.matmul(X, w)

mnist = input_data.read_data_sets("/tmp/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])

w = init_weights([784, 10])
y_model = model(X, w)


loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_model, labels=Y))

train_op = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

predict_op = tf.argmax(y_model,1)


with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)

    for i in range(100):
        for start, end in zip(range(0,len(trX),128), range(128, len(trX), 128)):
            sess.run(train_op, feed_dict={X:teX, Y:teY})


        print(i,np.mean(np.argmax(teY,axis=1) == sess.run(predict_op, feed_dict= {X:teX, Y:teY})))
Extracting /tmp/data\train-images-idx3-ubyte.gz
Extracting /tmp/data\train-labels-idx1-ubyte.gz
Extracting /tmp/data\t10k-images-idx3-ubyte.gz
Extracting /tmp/data\t10k-labels-idx1-ubyte.gz
0 0.8899
1 0.9029
2 0.9104
3 0.9154
4 0.9184
5 0.921
6 0.9235
7 0.9248
8 0.9256
9 0.9282
10 0.9293
11 0.9312
12 0.9326
13 0.9331
14 0.9339
15 0.9346
16 0.9357
17 0.9368
18 0.9371
19 0.9375
20 0.9382
21 0.9391
22 0.9394
23 0.9399
24 0.94
25 0.9402
26 0.9406
27 0.9412
28 0.9416
29 0.9423
30 0.9426
31 0.9433
32 0.9435
33 0.9438
34 0.9444
35 0.9445
36 0.9447
37 0.9452
38 0.9455
39 0.9455
40 0.9457
41 0.946
42 0.9458
43 0.9462
44 0.9467
45 0.947
46 0.9473
47 0.9478
48 0.948
49 0.9481
50 0.9484
51 0.9484
52 0.9491
53 0.9494
54 0.9496
55 0.95
56 0.95
57 0.95
58 0.9501
59 0.9501
60 0.9503
61 0.9509
62 0.9512
63 0.9513
64 0.9517
65 0.9518
66 0.9519
67 0.9521
68 0.9521
69 0.9521
70 0.9522
71 0.9525
72 0.9528
73 0.9529
74 0.9534
75 0.9536
76 0.9536
77 0.9538
78 0.9539
79 0.9543
80 0.9546
81 0.9546
82 0.9546
83 0.9545
84 0.9547
85 0.9547
86 0.9549
87 0.955
88 0.9554
89 0.9554
90 0.9556
91 0.9559
92 0.956
93 0.9562
94 0.9562
95 0.9564
96 0.9565
97 0.9566
98 0.9571
99 0.9573


Process finished with exit code 0
 
 
 
 
 
 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页