ํ๊ธฐ
CPU ๋๋น GPU๋ ์ง์ง 100๋ฐฐ๋ ์ฒด๊ฐ์ ๋น ๋ฅธ ์๋๋ก ํ์ต ์ฒ๋ฆฌ๊ฐ ๋๋ค.
๋ฉ๋ชจ๋ฆฌ์ ๋ฐ์ดํฐ๋ฅผ ์ฌ๋ ค์ ์งํํ๋ ์ค์ธ๋ฐ GPU๋ ์๊ฐ ์๊ฐ ๋ง์ด์ฌ์ฉ๋๋ค.
CNN ๊ณตํฌํ์์ง์๋ฅผ ์ผ์๋ณ๋ก ์ธํ ํ์ฌ ๊ทธ ๊ธฐ๊ฐ์ ๋ฐ์ํ ๊ฒ์๋ฌผ์ ์ ๋ชฉ์ ํด๋น ๊ฐ์ค์น๋ก ์ ์ฉํ์๋ค. ์ฆ ํน์ ๋ฌธ์ฅ์ด ๋ฑ์ฅํ๋ฉด ๊ณตํฌ์ฅ์ด๋ผ๋๊ฐ ํ์์ฅ์์ ์์ฃผ ๋ณด์๋ ๊ฒ์๊ธ๋ก ๋ณด์ ํธ๋ค๊ฐ ๋๋ ๋ค์์ฅ์ ์์ธกํ๋ ์ธ๊ฐ ์งํ๋ก ๋ง๋ค ์ ์์ ๊ฒ์ด๋ค.
์ ์ฒด ๋ฐ์ดํฐ ํ์ต์ธ Epoch๋ 50ํ๋ก ์ค์ ํ์๊ณ ์ ํ๋๋ ์ ์ ํฅ์๋๋ ์์์ ๋ณด์ธ๋ค. ์ด ํ์ต ์ํจ ๋ฐ์ดํฐ๋ 5๋ง๊ฑด์ผ๋ก ๋ค์ด๋ฒ์ ๋ฌดํ์คํฌ๋กค ์ ์ฌ์ฉ๋๋ API๋ฅผ ํ์ฉํ๋ค.
ํ๋ฒ ํธ์ถ ์ 50๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ํ๋ณดํ ์ ์์์ผ๋ฉฐ ์ต๋ 1000ํ์ด์ง๊น์ง ์กฐํ ์ฌ์ดํด์ด ๋๋ ๊ฒ์ผ๋ก ๋ณด์์ ์นดํ ํ๋๋น 5๋ง๊ฑด ํ๋์ด ๊ฐ๋ฅํ๋ค.
์ค์ ๋ฐ์ดํฐ๋ ์๋์ ์ฌ์ดํธ์์ ํ์ธํ ์ ์๋ค.
ํ์ต ์์ค์ฝ๋
์๊ฒฉ์ง ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ ๊ธฐ์กด ๋ชจ๋ธ์ ํ์ต์ํค๋ฉฐ loss 3ํ ๋ฌ์ฑ ์ ํ์ต ์ข ๋ฃ
import joblib
import pandas as pd
import requests
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset, DataLoader
# 1. ๋ฐ์ดํฐ ์ค๋น
def fetch_data(page):
url = f"http://localhost:3000/api/v1/parse/{page}"
response = requests.get(url)
result = response.json()
return pd.DataFrame(result['result'])
def load_data(pages):
all_data = pd.DataFrame()
for page in pages:
df = fetch_data(page)
all_data = pd.concat([all_data, df], ignore_index=True)
return all_data
# ํ์ด์ง ๋ฒ์ ์ค์
pages = range(1, 996) # 1๋ถํฐ 995๊น์ง ํ์ด์ง
df = load_data(pages)
# 2. ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(df['title']).toarray()
y = df['fear_greed_index'].values
# CountVectorizer ์ ์ฅ
joblib.dump(vectorizer, 'vectorizer.pkl')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
class FearGreedDataset(Dataset):
def __init__(self, X, y):
self.X = X
self.y = y
def __len__(self):
return len(self.y)
def __getitem__(self, idx):
return torch.tensor(self.X[idx], dtype=torch.float32), torch.tensor(self.y[idx], dtype=torch.float32)
train_dataset = FearGreedDataset(X_train, y_train)
test_dataset = FearGreedDataset(X_test, y_test)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
# 3. ๋ชจ๋ธ ์ ์
class FearGreedModel(nn.Module):
def __init__(self, input_dim):
super(FearGreedModel, self).__init__()
self.fc1 = nn.Linear(input_dim, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 1)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
# ๋๋ฐ์ด์ค ์ค์
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# ๋ชจ๋ธ ์ด๊ธฐํ
def initialize_model(model_path=None):
input_dim = X_train.shape[1] # ํ์ฌ ๋ฐ์ดํฐ์ ์
๋ ฅ ์ฐจ์
model = FearGreedModel(input_dim).to(device) # ๋ชจ๋ธ์ ๋๋ฐ์ด์ค๋ก ์ด๋
if model_path:
try:
model.load_state_dict(torch.load(model_path, map_location=device)) # map_location์ ์ฌ์ฉํ์ฌ ๋๋ฐ์ด์ค ์ค์
print('Model loaded from', model_path)
except RuntimeError as e:
print('Error loading model:', e)
# ๋ชจ๋ธ ๋ก๋ ์คํจ ์ ์ด๊ธฐํ
model = FearGreedModel(input_dim).to(device)
return model
# ์ ๊ทํ์ต
# model = initialize_model()
# ์ฌํ์ต
model = initialize_model('fear_greed_model_gpu.pth')
# 4. ๋ชจ๋ธ ํ์ต
def train_model():
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
loss2 = 0
epoch = 0
while True: # Infinite loop
model.train()
for X_batch, y_batch in train_loader:
X_batch, y_batch = X_batch.to(device), y_batch.to(device) # ๋ฐฐ์น๋ฅผ ๋๋ฐ์ด์ค๋ก ์ด๋
optimizer.zero_grad()
outputs = model(X_batch)
loss = criterion(outputs.squeeze(), y_batch)
loss.backward()
optimizer.step()
print(f'Epoch {epoch + 1}, Loss: {loss.item()}')
epoch += 1
if (loss.item() < 2.0):
print('Early stopping')
loss2 += 1
if (loss2 == 3):
break
train_model() # ํ์ํ ์ํญ ์๋ฅผ ์ค์ ํฉ๋๋ค.
# ๋ชจ๋ธ ์ ์ฅ
torch.save(model.state_dict(), 'fear_greed_model_gpu.pth')
print('Model saved.')
# 5. ์์ธก
model.eval()
with torch.no_grad():
test_loss = 0
for X_batch, y_batch in test_loader:
X_batch, y_batch = X_batch.to(device), y_batch.to(device) # ๋ฐฐ์น๋ฅผ ๋๋ฐ์ด์ค๋ก ์ด๋
outputs = model(X_batch)
loss = nn.MSELoss()(outputs.squeeze(), y_batch) # criterion์ ์ฌ๊ธฐ์ ์ง์ ์ ์
test_loss += loss.item()
print(f'Test Loss: {test_loss / len(test_loader)}')
# ์๋ก์ด ์ ๋ชฉ์ ๋ํ ์์ธก
new_titles = ['ํญ๋ฝ์ฅ ์ด๋ค์']
new_X = vectorizer.transform(new_titles).toarray()
new_X_tensor = torch.tensor(new_X, dtype=torch.float32).to(device) # ์
๋ ฅ ํ
์๋ฅผ ๋๋ฐ์ด์ค๋ก ์ด๋
model.eval()
with torch.no_grad():
prediction = model(new_X_tensor).item()
print(f'Predicted Fear-Greed Index: {prediction}')
API ์๋ฒ
์คํ - uvicorn.exe main:app --reload
import joblib
import torch
import torch.nn as nn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
# FastAPI ์ธ์คํด์ค ์์ฑ
app = FastAPI()
# ๋ชจ๋ธ ์ ์
class FearGreedModel(nn.Module):
def __init__(self, input_dim):
super(FearGreedModel, self).__init__()
self.fc1 = nn.Linear(input_dim, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 1)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
# ๋๋ฐ์ด์ค ์ค์
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# ๋ชจ๋ธ ๋ฐ CountVectorizer ๋ก๋
def initialize_model(input_dim, model_path):
model = FearGreedModel(input_dim).to(device)
model.load_state_dict(torch.load(model_path, map_location=device))
model.eval() # ํ๊ฐ ๋ชจ๋๋ก ์ ํ
return model
# ์ ์ฅ๋ CountVectorizer ๋ก๋
vectorizer = joblib.load('vectorizer.pkl')
# ๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ
model = initialize_model(input_dim=len(vectorizer.get_feature_names_out()), model_path='fear_greed_model_gpu.pth')
# ์์ฒญ ๋ชจ๋ธ ์ ์
class PredictRequest(BaseModel):
titles: list
# FastAPI ์๋ํฌ์ธํธ ์ ์
@app.post("/predict")
def predict(request: PredictRequest):
try:
new_X = vectorizer.transform(request.titles).toarray()
new_X_tensor = torch.tensor(new_X, dtype=torch.float32).to(device)
with torch.no_grad():
predictions = model(new_X_tensor).cpu().numpy().flatten()
return {"predictions": predictions.tolist()}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ๋ชจ๋ธ ํ๊ฐ ์๋ํฌ์ธํธ ์ ์
ํ ์คํธ ์์
### GET request to example server
POST http://localhost:8000/predict
Content-Type: application/json
{
"titles": [
"์ฐ๋ฆฌ๋ ๋งํ๋ค"
]
}
###
'๐ํ๋ก๊ทธ๋๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ธฐํ] Ai model์ ํตํ ์ธ๊ฐ์งํ ์ถ์ข ์ง์ ๊ฐ๋ฐ (0) | 2024.08.03 |
---|---|
[์ ๋ณด] ํ๋ก์ ํธ AIRA ์งํ ํํฉ (0) | 2024.07.27 |
[golang] ๊ธฐ์ด (0) | 2024.01.29 |
[์ ๋ณด] kubectl _get_comp_words_by_ref: command not found (0) | 2024.01.16 |
[์ ๋ณด] ํฌ๋กค๋ง ์ฃผ์์ฌํญ (0) | 2024.01.05 |