DNN_only code πŸ’»

창슈·2025λ…„ 4μ›” 15일

Deep Learning

λͺ©λ‘ 보기
16/16
post-thumbnail

DNN(심측 신경망) μ‘μš©μ˜ˆμ œ


πŸ“¦ MNIST 필기체 숫자 인식

import matplotlib.pyplot as plt
import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(512, activation='relu'))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
Epoch 1/5
60000/60000 [======================] - 7s 116us/sample - loss: 0.2205 - acc: 0.9348
Epoch 2/5
60000/60000 [======================] - 7s 110us/sample - loss: 0.0969 - acc: 0.9700
Epoch 3/5
60000/60000 [======================] - 7s 109us/sample - loss: 0.0678 - acc: 0.9785
Epoch 4/5
60000/60000 [======================] - 6s 108us/sample - loss: 0.0529 - acc: 0.9834
Epoch 5/5
60000/60000 [======================] - 7s 108us/sample - loss: 0.0428 - acc: 0.9859
10000/10000 [======================] - 0s 43us/sample - loss: 0.0645 - acc: 0.9795

πŸ“¦ νŒ¨μ…˜ μ•„μ΄ν…œ λΆ„λ₯˜

  • ν…μ„œν”Œλ‘œμš° νŠœν† λ¦¬μ–Όμ— λ‚˜μ˜€λŠ” νŒ¨μ…˜ μ•„μ΄ν…œμ„ 심측 μ‹ κ²½λ§μœΌλ‘œ λΆ„λ₯˜ν•˜λŠ” μ½”λ“œ

  • νŒ¨μ…˜ MNIST 데이터셋은 10개의 λ²”μ£Ό(category)의 70,000개의 νŒ¨μ…˜ κ΄€λ ¨ 이미지 (옷, ꡬ두, ν•Έλ“œλ°± λ“±)κ°€ 제곡되며 ν•΄μƒλ„λŠ” 28x28이닀.

  • μ΄λ―Έμ§€λŠ” 28x28 크기, ν”½μ…€ 값은 0κ³Ό 255 사이

  • λ ˆμ΄λΈ”(label)은 0μ—μ„œ 9κΉŒμ§€μ˜ μ •μˆ˜λ‘œμ„œ νŒ¨μ…˜ μ•„μ΄ν…œμ˜ λ²”μ£Όλ₯Ό λ‚˜νƒ€λƒ„

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras import datasets, layers, models

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

plt.imshow(train_images[0])

train_images = train_images / 255.0
test_images = test_images / 255.0

model = models.Sequential()
model.add(layers.Flatten(input_shape=(28, 28)))
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)
print('정확도:', test_acc)
10000/10000 [==============================] - 0s 32us/sample - loss: 0.3560 -
acc: 0.8701
정확도: 0.8701

πŸ“¦ 타이타닉 μƒμ‘΄μž μ˜ˆμΈ‘ν•˜κΈ°

  • Kaggle μ‚¬μ΄νŠΈμ—μ„œ κ²½μ§„λŒ€νšŒ μ°Έμ‘° ("μ–΄λ–€ λΆ€λ₯˜μ˜ μ‚¬λžŒλ“€μ˜ 생쑴λ₯ μ΄ λ†’μ•˜μ„κΉŒ?" 에 λŒ€ν•œ 예츑)
# 라이브러리 적재
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
# ν•™μŠ΅ 데이터 λ‹€μš΄λ‘œλ“œ
# 타이타닉 λ°μ΄ν„°λŠ” Kaggle μ‚¬μ΄νŠΈ(https://www.Kaggle.com/c/titanic/data)μ—μ„œ λ‹€μš΄λ‘œλ“œ λ°›λŠ”λ‹€.
# ν•™μŠ΅λ°μ΄ν„°λŠ” train.csv파일이고 ν…ŒμŠ€νŠΈ λ°μ΄ν„°λŠ” test.csvνŒŒμΌμ΄λ‹€.

# 데이터 μ„ΈνŠΈλ₯Ό 읽어듀인닀.
train = pd.read_csv("train.csv", sep=',')
test = pd.read_csv("test.csv", sep=',')

# ν•„μš”μ—†λŠ” μ»¬λŸΌμ„ μ‚­μ œν•œλ‹€.
train.drop(['SibSp', 'Parch', 'Ticket', 'Embarked', 'Name', 'Cabin', 'PassengerId', 'Fare', 'Age'], inplace=True, axis=1)

# κ²°μ†μΉ˜κ°€ μžˆλŠ” 데이터 행은 μ‚­μ œν•œλ‹€.
train.dropna(inplace=True)
# ꡬ글 λ“œλΌμ΄λΈŒ μ—°κ²°
from google.colab import drive
drive.mount('/content/drive')
# Mounted at /content/drive
import os
print(os.listdir('/content/drive/MyDrive'))
# ['Colab Notebooks', 'Google Form’,…]
# 성별에 λ”°λ₯Έ 생쑴λ₯  μ‹œκ°ν™”
df = train.groupby('Sex').mean()["Survived"]
df.plot(kind='bar')
plt.show()

# Pcalss에 λŒ€ν•΄μ„œλ„ 생쑴λ₯  κ·Έλž˜ν”„λ₯Ό 그렀보면 μƒλ‹Ήν•œ 인과관계가 μžˆλ‹€λŠ” 것을 μ•Œ 수 μžˆλ‹€.
#λ”°λΌμ„œ ν•™μŠ΅μ˜ μž…λ ₯으둜 β€œSex”와 β€œPclassβ€λ§Œμ„ κ³ λ €ν•œλ‹€.

μ—¬μ„±μ˜ 생쑴λ₯ μ΄ 높은 것을 μ•Œ 수 있음

# ν•™μŠ΅ 데이터 μ •μ œ
# train.drop(['SibSp', 'Parch', 'Ticket', 'Embarked', 'Name', 'Cabin', 'PassengerId', 'Fare', 'Age'], inplace=True, axis=1)
# inplace=TrueλŠ” μ›λž˜ 데이터 ν”„λ ˆμž„μ„ λ³€κ²½ν•˜λΌλŠ” 의미이고 axis=1은 μΆ•λ²ˆν˜Έ 1번 (즉 column)을 μ‚­μ œν•˜λΌλŠ” μ˜λ―Έμ΄λ‹€.

train.head()
# Survived Pclass Sex
# 0 0 3 male
# 1 1 1 female
# 2 1 3 female
# 3 1 1 female
# 4 0 3 male
# 성별 기호λ₯Ό 수치둜 λ³€ν™˜ν•œλ‹€.
# λ”₯λŸ¬λ‹μ€ 0λΆ€ν„° 1μ‚¬μ΄μ˜ μ‹€μˆ˜λ§Œ 처리 κ°€λŠ₯
for ix in train.index:
	if train.loc[ix, 'Sex']=="male":
		train.loc[ix, 'Sex']=1
	else:
		train.loc[ix, 'Sex']=0

# 2차원 배열을 1차원 λ°°μ—΄λ‘œ ν‰νƒ„ν™”ν•œλ‹€.
target = np.ravel(train.Survived)

# 생쑴여뢀λ₯Ό ν•™μŠ΅ λ°μ΄ν„°μ—μ„œ μ‚­μ œν•œλ‹€.
train.drop(['Survived'], inplace=True, axis=1)
train = train.astype(float) # 졜근 μ†ŒμŠ€μ—μ„œλŠ” floatν˜•νƒœλ‘œ ν˜•λ³€ν™˜ν•˜μ—¬μ•Ό ν•œλ‹€.
# μΌ€λΌμŠ€ λͺ¨λΈμ„ μƒμ„±ν•œλ‹€.
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(2,)))
model.add(tf.keras.layers.Dense(8, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

# μΌ€λΌμŠ€ λͺ¨λΈμ„ μ»΄νŒŒμΌν•œλ‹€.
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# μΌ€λΌμŠ€ λͺ¨λΈμ„ ν•™μŠ΅μ‹œν‚¨λ‹€.
model.fit(train, target, epochs=30, batch_size=1, verbose=1)
...
Epoch 29/30
891/891 [==============================] - 1s 753us/sample - loss: 0.4591 - acc: 0.7677
Epoch 30/30
891/891 [==============================] - 1s 753us/sample - loss: 0.4547 - acc: 0.7789
profile
🐾

1개의 λŒ“κΈ€

comment-user-thumbnail
2025λ…„ 5μ›” 14일

정말 도움이 λ˜λŠ” μ •λ³΄μ˜€μŠ΅λ‹ˆλ‹€.
항상 즐겨보고 μžˆμŠ΅λ‹ˆλ‹€.
κ°μ‚¬ν•©λ‹ˆλ‹€.

λ‹΅κΈ€ 달기