본문 바로가기
프로그램

[파이썬] 임의의 5개의 정수와 이 정수들의 합들로 이루어진 x데이터로 학습해서 임의의 5개 정수 예측하기

by 오디세이99 2023. 5. 10.
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
반응형

댓글