1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import tensorflow as tf batch_size = 128 # °¡ÁßÄ¡¸¦ º¯°æÇϱâ Àü¿¡ ó¸®ÇÏ´Â »ùÇÃÀÇ °³¼ö num_classes = 10 # Ãâ·Â Ŭ·¡½ºÀÇ °³¼ö epochs = 20 # ¿¡Æ÷Å©ÀÇ °³¼ö # µ¥ÀÌÅ͸¦ ÇнÀ µ¥ÀÌÅÍ¿Í Å×½ºÆ® µ¥ÀÌÅÍ·Î ³ª´«´Ù. (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # ÀÔ·Â À̹ÌÁö¸¦ 2Â÷¿ø¿¡¼ 1Â÷¿ø º¤ÅÍ·Î º¯°æÇÑ´Ù. x_train = x_train.reshape(60000, 784) x_test = x_test.reshape(10000, 784) # ÀÔ·Â À̹ÌÁöÀÇ Çȼ¿ °ªÀÌ 0.0¿¡¼ 1.0 »çÀÌÀÇ °ªÀÌ µÇ°Ô ÇÑ´Ù. x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 # Ŭ·¡½ºÀÇ °³¼ö¿¡ µû¶ó¼ ÇϳªÀÇ Ãâ·Â Çȼ¿¸¸ÀÌ 1ÀÌ µÇ°Ô ÇÑ´Ù. # ¿¹¸¦ µé¸é 1 0 0 0 0 0 0 0 0 0°ú °°´Ù. y_train = tf.keras.utils.to_categorical(y_train, num_classes) y_test = tf.keras.utils.to_categorical(y_test, num_classes) # ½Å°æ¸ÁÀÇ ¸ðµ¨À» ±¸ÃàÇÑ´Ù. model = tf.keras.models.Sequential() model.add(tf.keras.layers.Dense(512, activation='sigmoid', input_shape=(784,))) model.add(tf.keras.layers.Dense(num_classes, activation='sigmoid')) model.summary() sgd = tf.keras.optimizers.SGD(lr=0.1) # ¼Õ½Ç ÇÔ¼ö¸¦ Á¦°ö ¿ÀÂ÷ ÇÔ¼ö·Î ¼³Á¤ÇÏ°í ÇнÀ ¾Ë°í¸®ÁòÀº SGD ¹æ½ÄÀ¸·Î ÇÑ´Ù. model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy']) # ÇнÀÀ» ¼öÇàÇÑ´Ù. history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs) # ÇнÀÀ» Æò°¡ÇÑ´Ù. score = model.evaluate(x_test, y_test, verbose=0) print('Å×½ºÆ® ¼Õ½Ç°ª:', score[0]) print('Å×½ºÆ® Á¤È®µµ:', score[1]) | cs |