텐서플로우에서 제공하는 툴을 이용해 MNIST 데이터를 다운받습니다.


In [1]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

In [3]:
import tensorflow as tf

In [4]:
tf.convert_to_tensor(mnist.train.images).get_shape()


Out[4]:
TensorShape([Dimension(55000), Dimension(784)])

가중치 텐서와 바이어스 텐서를 만듭니다.


In [2]:
import tensorflow as tf

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

훈련 이미지 데이터를 넣을 플레이스홀더와 소프트맥스 텐서를 만듭니다.


In [3]:
x = tf.placeholder("float", [None, 784])
y = tf.nn.softmax(tf.matmul(x,W) + b)

실제 레이블을 담기위한 텐서와 교차 엔트로피 방식을 이용하는 그래디언트 디센트 방식을 선택합니다.


In [4]:
y_ = tf.placeholder("float", [None,10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

변수를 초기화하고 세션을 시작합니다.


In [5]:
sess = tf.Session()
sess.run(tf.initialize_all_variables())

1000의 반복을 수행하고 결과를 출력합니다. 최종 정확도는 91% 정도 입니다.


In [7]:
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))


0.4075
0.3573
0.5091
0.4416
0.5773
0.519
0.613
0.6506
0.7404
0.8002
0.8245
0.7839
0.692
0.7013
0.7577
0.7891
0.7871
0.8041
0.8135
0.7161
0.6986
0.7697
0.7746
0.7777
0.8541
0.825
0.814
0.8054
0.7779
0.8358
0.8402
0.8028
0.84
0.8404
0.8309
0.8421
0.8549
0.8157
0.7435
0.8559
0.8656
0.8628
0.8554
0.8338
0.8381
0.8484
0.8503
0.8619
0.8646
0.8435
0.8545
0.8758
0.7996
0.8518
0.8405
0.8553
0.8642
0.8737
0.8841
0.8848
0.8795
0.8732
0.8403
0.8724
0.8751
0.8606
0.8729
0.8573
0.8545
0.8261
0.8494
0.8066
0.868
0.8388
0.8787
0.826
0.8556
0.8805
0.8821
0.8888
0.8624
0.8667
0.8747
0.8852
0.8875
0.8876
0.8818
0.8885
0.8817
0.871
0.8047
0.8312
0.845
0.8578
0.8671
0.8565
0.7704
0.8124
0.8864
0.892
0.894
0.871
0.8736
0.8674
0.8832
0.8954
0.8911
0.8973
0.8932
0.8902
0.8942
0.8857
0.8955
0.8942
0.8697
0.8859
0.8957
0.8979
0.8988
0.8905
0.8961
0.8983
0.896
0.8939
0.8695
0.8817
0.8947
0.8852
0.8973
0.8899
0.8968
0.9008
0.8766
0.9009
0.8612
0.9009
0.8971
0.9028
0.9031
0.9028
0.9022
0.8989
0.8806
0.8835
0.8816
0.8514
0.9019
0.9035
0.8851
0.8447
0.9015
0.8884
0.8527
0.8887
0.8933
0.8957
0.8953
0.8704
0.878
0.8795
0.8817
0.8993
0.9083
0.8897
0.8882
0.9011
0.8947
0.9064
0.906
0.9014
0.9012
0.8888
0.902
0.9016
0.8843
0.8822
0.8853
0.8885
0.8961
0.8909
0.8888
0.9023
0.9072
0.9018
0.8866
0.9034
0.8652
0.893
0.9038
0.9014
0.8999
0.8831
0.8783
0.9055
0.9061
0.8877
0.8593
0.8846
0.8957
0.9027
0.8989
0.8975
0.9026
0.8983
0.9048
0.9003
0.8921
0.9028
0.9057
0.9078
0.9037
0.9093
0.9103
0.9036
0.9066
0.9017
0.9022
0.8814
0.8741
0.9039
0.8941
0.8964
0.8679
0.8969
0.901
0.9025
0.9018
0.9005
0.9
0.8999
0.9061
0.9011
0.8852
0.8994
0.8991
0.9047
0.8959
0.8957
0.9088
0.9074
0.9045
0.8927
0.9067
0.8917
0.8993
0.9013
0.9063
0.8955
0.897
0.8952
0.8878
0.9004
0.901
0.9104
0.9111
0.9045
0.9053
0.9074
0.8863
0.8919
0.9079
0.9065
0.8931
0.9056
0.9105
0.8904
0.9104
0.8825
0.8974
0.9014
0.9064
0.882
0.9061
0.8352
0.8878
0.9076
0.906
0.8974
0.8989
0.9029
0.8886
0.8837
0.8614
0.9073
0.9043
0.9015
0.9058
0.8703
0.9009
0.9122
0.9096
0.905
0.9116
0.9052
0.8941
0.8038
0.8248
0.8955
0.8635
0.8811
0.9012
0.882
0.9094
0.8996
0.8996
0.882
0.8993
0.9062
0.9093
0.9071
0.9026
0.9088
0.9001
0.9076
0.8976
0.8969
0.9043
0.898
0.8946
0.8993
0.9039
0.8864
0.9049
0.8746
0.8733
0.8376
0.8834
0.8841
0.8954
0.9013
0.9018
0.9023
0.9066
0.8905
0.9089
0.906
0.9056
0.9088
0.9081
0.9011
0.9116
0.9081
0.8987
0.8634
0.828
0.8988
0.8864
0.9095
0.893
0.8958
0.9
0.9017
0.902
0.8982
0.887
0.8984
0.9118
0.9083
0.9061
0.9033
0.9103
0.9039
0.8938
0.899
0.9079
0.9031
0.9082
0.9028
0.9054
0.9061
0.9018
0.8538
0.8722
0.9065
0.9014
0.9001
0.9068
0.9042
0.9063
0.909
0.8945
0.8997
0.9042
0.908
0.908
0.902
0.9066
0.8897
0.8997
0.9031
0.9134
0.9086
0.9109
0.9058
0.9061
0.9057
0.9131
0.9049
0.9088
0.8916
0.904
0.8968
0.8965
0.9095
0.9088
0.9097
0.9055
0.9033
0.9046
0.8984
0.9009
0.8855
0.8912
0.9051
0.911
0.9115
0.889
0.9025
0.8933
0.9056
0.9053
0.9004
0.9035
0.9023
0.8964
0.8894
0.8805
0.9024
0.9098
0.8977
0.9111
0.9108
0.9132
0.8994
0.9149
0.9019
0.8942
0.9106
0.9141
0.8791
0.908
0.909
0.9048
0.9126
0.9123
0.9093
0.9074
0.9015
0.8996
0.9093
0.9039
0.9123
0.9056
0.9154
0.8915
0.9132
0.9038
0.8822
0.8299
0.9061
0.9143
0.9032
0.8337
0.8922
0.9056
0.9096
0.9056
0.9018
0.9111
0.9044
0.9085
0.9042
0.8977
0.9042
0.9037
0.915
0.9133
0.9051
0.9095
0.8847
0.886
0.9085
0.9151
0.9127
0.9143
0.9085
0.9061
0.9075
0.8959
0.9002
0.9078
0.9082
0.9146
0.908
0.8733
0.8932
0.9091
0.9077
0.8978
0.9004
0.9105
0.9087
0.8956
0.9076
0.915
0.8973
0.9055
0.9103
0.9048
0.9079
0.9142
0.9155
0.9124
0.9074
0.9129
0.9145
0.9119
0.9016
0.9108
0.9107
0.9058
0.9151
0.9014
0.9065
0.9133
0.9
0.8968
0.9069
0.9135
0.9094
0.9124
0.9131
0.9105
0.9088
0.8909
0.9126
0.9158
0.9072
0.9088
0.9111
0.9112
0.9089
0.9035
0.8794
0.8879
0.8852
0.8933
0.8841
0.8914
0.8968
0.9103
0.9087
0.9118
0.9098
0.9084
0.9059
0.9054
0.849
0.8725
0.9043
0.9105
0.9145
0.9125
0.9099
0.9199
0.9147
0.9115
0.911
0.9062
0.9104
0.9144
0.9049
0.9098
0.9115
0.8981
0.9119
0.9141
0.9095
0.9095
0.9082
0.9102
0.908
0.9112
0.9042
0.9117
0.9145
0.9129
0.9158
0.9143
0.9157
0.9131
0.915
0.9162
0.9179
0.9033
0.9063
0.9076
0.9163
0.905
0.9036
0.9019
0.9133
0.9143
0.9134
0.9018
0.8943
0.9145
0.9104
0.9114
0.9138
0.9126
0.8868
0.9128
0.9045
0.8874
0.9141
0.9096
0.9171
0.9174
0.9108
0.9161
0.9115
0.9162
0.9184
0.9158
0.9169
0.8856
0.9109
0.9165
0.9164
0.914
0.9179
0.9166
0.9142
0.9138
0.9152
0.9054
0.9148
0.9153
0.9143
0.9125
0.9133
0.9165
0.9151
0.9088
0.9134
0.8935
0.9116
0.9096
0.915
0.9146
0.9136
0.9063
0.9082
0.909
0.9049
0.8994
0.9123
0.9104
0.9051
0.8783
0.8881
0.9077
0.8926
0.9135
0.9157
0.9115
0.9142
0.9132
0.9124
0.9136
0.9147
0.9125
0.9143
0.9167
0.9081
0.904
0.905
0.902
0.9099
0.9146
0.9096
0.9094
0.9053
0.9115
0.9147
0.9158
0.9137
0.9091
0.9095
0.9145
0.9131
0.893
0.9119
0.9122
0.914
0.912
0.9143
0.9104
0.9107
0.9136
0.9126
0.9006
0.9085
0.9075
0.9141
0.9179
0.9119
0.9083
0.9179
0.9092
0.9146
0.9174
0.9078
0.9155
0.9155
0.9143
0.9141
0.9162
0.9154
0.9145
0.9036
0.9147
0.9166
0.9126
0.9007
0.9158
0.9178
0.9154
0.914
0.917
0.9167
0.9016
0.9082
0.8959
0.8877
0.9011
0.9099
0.9146
0.902
0.9075
0.9098
0.9142
0.917
0.9108
0.9136
0.9169
0.9174
0.9062
0.8953
0.8883
0.9042
0.9048
0.8956
0.9136
0.9118
0.9048
0.9116
0.9063
0.9193
0.9145
0.9154
0.9008
0.9186
0.9112
0.9134
0.9104
0.9169
0.9137
0.9132
0.9113
0.9128
0.9189
0.9189
0.9118
0.9131
0.9102
0.9134
0.9127
0.8983
0.9172
0.9115
0.9206
0.9054
0.9163
0.9108
0.9224
0.9186
0.9158
0.9139
0.9176
0.9157
0.918
0.9079
0.9141
0.9141
0.9107
0.9124
0.9169
0.9067
0.9112
0.9123
0.9127
0.9136
0.9097
0.9069
0.9001
0.9072
0.9088
0.908
0.915
0.9071
0.9163
0.9093
0.9136
0.9152
0.9163
0.9135
0.9021
0.9184
0.9117
0.9151
0.9151
0.9153
0.908
0.9142
0.9104
0.9086
0.9139
0.9095
0.9091
0.9178
0.9096
0.9173
0.9085
0.8907
0.9151
0.9175
0.9194
0.9138
0.9061
0.9159
0.9075
0.9134
0.9174
0.8987
0.8988
0.9149
0.912
0.919
0.9152
0.921
0.9178
0.9054
0.9118
0.9104
0.9036
0.9149
0.9147
0.9093
0.913
0.9088
0.9187
0.9112
0.9128
0.9103
0.9138
0.908
0.9082
0.9114
0.9135
0.9182
0.9054
0.915
0.9184
0.9168
0.9159
0.9179
0.9165
0.9198
0.9092
0.9157
0.9174
0.9184
0.9168
0.9166
0.9141
0.8938
0.9132
0.8926
0.9191
0.9159
0.9147
0.9184
0.918
0.9103
0.9154
0.9182
0.9157
0.9197
0.9032
0.8982
0.9155
0.9168
0.9151
0.9045
0.9132
0.9167
0.9172
0.9189
0.9155
0.9173
0.9105
0.9126
0.912
0.9136
0.9052
0.9164
0.9086
0.9082
0.904
0.9105
0.9121
0.9114
0.9113
0.905
0.918
0.922
0.9183
0.9167
0.9157
0.9151
0.916
0.8909
0.8921
0.9133
0.9192
0.9193
0.8897
0.9074
0.9043
0.9169
0.9057
0.9147
0.9139
0.9076
0.9181
0.903
0.9118
0.9169
0.8967
0.9142
0.9077
0.9156
0.9184
0.9217
0.9209
0.916
0.911
0.9137
0.9133
0.913
0.9143
0.9179
0.9184
0.9232
0.9233
0.9177
0.9172
0.8964
0.9101
0.9156
0.9159
0.9125
0.9192
0.9159
0.9098
0.9175
0.9114
0.9209
0.9198
0.9096
0.9189
0.9183
0.9189
0.9149
0.9195
0.919
0.9189
0.921
0.9072
0.9172
0.9043
0.9165
0.9159
0.9129
0.9082
0.9049
0.9107
0.9114