728x90
반응형
A,B,C,D,E 5개의 정수가 있을때,
A + B, A + C, A + D, A + E, B + C, B + D, B + E, C + D, C + E, D + E 인 데이터를 가지고 A~E 정수를 찾는 딥러닝 코드.
import numpy as np
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
# 텐서플로가 첫 번째 GPU만 사용하도록 제한
try:
tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)
except RuntimeError as e:
# 프로그램 시작시에 접근 가능한 장치가 설정되어야만 합니다
print(e)
# 데이터 생성
A = np.random.randint(0, 100, size=1000)
B = np.random.randint(0, 100, size=1000)
C = np.random.randint(0, 100, size=1000)
D = np.random.randint(0, 100, size=1000)
E = np.random.randint(0, 100, size=1000)
sums = np.array([A + B, A + C, A + D, A + E, B + C, B + D, B + E, C + D, C + E, D + E]).T
integers = np.array([A, B, C, D, E]).T
# 데이터 분할
train_size = int(0.8 * len(sums))
x_train = sums[:train_size]
x_test = sums[train_size:]
y_train = integers[:train_size]
y_test = integers[train_size:]
# 모델 정의
inputs = Input(shape=(10,))
x = Dense(32, activation='relu')(inputs)
x = Dense(32, activation='relu')(x)
x = Dense(32, activation='relu')(x)
outputs = Dense(5, activation='linear')(x)
model = Model(inputs=inputs, outputs=outputs)
# 컴파일 및 훈련
model.compile(optimizer=Adam(learning_rate=0.001), loss='mse')
model.fit(x_train, y_train, epochs=100, batch_size=32, validation_data=(x_test, y_test))
# 예측
# x_new = np.array([[150, 180, 190, 200, 220, 240, 250, 260, 280, 290]])
x_new = x_test[0].reshape((1,len(x_test[0])))
y_real = y_test[0]
y_pred = model.predict(x_new)
print("real x integers: ", x_new)
print("real integers: ", y_real)
print("Predicted integers: ", y_pred)
-
real x integers: [[ 76 73 95 132 11 33 70 30 67 89]]
real integers: [69 7 4 26 63]
Predicted integers: [[69.309944 7.1309047 4.2281637 25.78726 62.70533 ]]
728x90
반응형
'프로그램' 카테고리의 다른 글
[파이썬] 터틀(turtle) 임의의 다각형,색,좌표로 그리기 (0) | 2023.05.10 |
---|---|
[파이썬] 클래스의 스페셜메소드(__call__) (0) | 2023.05.10 |
[C언어] 문제 : 컴퓨터와 사람이 1~5 사이의 수를 입력해 합이 31에 먼저 되는 쪽이 이기는 게임 (0) | 2023.05.10 |
[C언어] 문제 : 키와 체중을 입력 받아 BMI 계산하고, 비만 구분 (0) | 2023.05.10 |
[C언어] 문제 : 4개의 정수 입력 받아 가장 작은 수 찾기 (0) | 2023.05.10 |
댓글