123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- from datasets import Datasets
- from exts import get_one_hot
- import tensorflow.compat.v1 as tf
- tf.disable_v2_behavior()
- def main():
-
- train_data, vaild_data, test_data = Datasets.load_mnist()
- x_train, y_train = train_data
- x_test, y_test = test_data
-
- y_train = get_one_hot(y_train)
- y_test = get_one_hot(y_test)
-
- batch_size = 100
-
- x = tf.placeholder("float", [None, 784])
-
- y_ = tf.placeholder("float", [None, 10])
-
- W = tf.Variable(tf.zeros([784, 10]))
-
- b = tf.Variable(tf.zeros([10]))
-
- y = tf.nn.softmax(tf.matmul(x, W) + b)
-
- cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
- train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
-
- init = tf.initialize_all_variables()
- sess = tf.Session()
- sess.run(init)
-
- for i in range(int(len(x_train) / batch_size)):
- batch_xs = x_train[(i * batch_size):((i + 1) * batch_size)]
- batch_ys = y_train[(i * batch_size):((i + 1) * batch_size)]
- 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, tf.float32))
- print(sess.run(accuracy, feed_dict={x: x_test, y_: y_test}))
- if __name__ == "__main__":
- main()
|