refactoring, predictions WIP
This commit is contained in:
@ -52,34 +52,106 @@ except ImportError:
|
||||
from NN.models.cnn_model import CNNModel
|
||||
except ImportError:
|
||||
# Create fallback CNN model for development/testing
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
class FallbackCNNModel(nn.Module):
|
||||
def __init__(self, input_shape=(900, 50), output_size=10):
|
||||
super().__init__()
|
||||
self.input_shape = input_shape
|
||||
self.output_size = output_size
|
||||
|
||||
# Simple CNN architecture for fallback
|
||||
self.conv1 = nn.Conv1d(input_shape[1], 32, kernel_size=3, padding=1)
|
||||
self.conv2 = nn.Conv1d(32, 64, kernel_size=3, padding=1)
|
||||
self.conv3 = nn.Conv1d(64, 128, kernel_size=3, padding=1)
|
||||
self.pool = nn.MaxPool1d(2)
|
||||
self.dropout = nn.Dropout(0.2)
|
||||
|
||||
# Calculate flattened size after convolutions and pooling
|
||||
conv_output_size = input_shape[0] // 8 * 128 # After 3 pooling layers
|
||||
|
||||
self.fc1 = nn.Linear(conv_output_size, 256)
|
||||
self.fc2 = nn.Linear(256, output_size)
|
||||
|
||||
logger.info(f"Fallback CNN Model initialized: input_shape={input_shape}, output_size={output_size}")
|
||||
|
||||
def forward(self, x):
|
||||
# Input shape: [batch, seq_len, features] -> [batch, features, seq_len]
|
||||
x = x.transpose(1, 2)
|
||||
|
||||
x = F.relu(self.conv1(x))
|
||||
x = self.pool(x)
|
||||
x = self.dropout(x)
|
||||
|
||||
x = F.relu(self.conv2(x))
|
||||
x = self.pool(x)
|
||||
x = self.dropout(x)
|
||||
|
||||
x = F.relu(self.conv3(x))
|
||||
x = self.pool(x)
|
||||
x = self.dropout(x)
|
||||
|
||||
# Flatten
|
||||
x = x.view(x.size(0), -1)
|
||||
|
||||
x = F.relu(self.fc1(x))
|
||||
x = self.dropout(x)
|
||||
x = self.fc2(x)
|
||||
|
||||
return x
|
||||
|
||||
class CNNModel:
|
||||
def __init__(self, input_shape=(900, 50), output_size=10):
|
||||
self.input_shape = input_shape
|
||||
self.output_size = output_size
|
||||
self.model = None
|
||||
self.model = FallbackCNNModel(input_shape, output_size)
|
||||
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=0.001)
|
||||
self.criterion = nn.CrossEntropyLoss()
|
||||
logger.info(f"Fallback CNN Model initialized: input_shape={input_shape}, output_size={output_size}")
|
||||
|
||||
def build_model(self, **kwargs):
|
||||
logger.info("Fallback CNN Model build_model called - using dummy model")
|
||||
logger.info("Fallback CNN Model build_model called - using PyTorch model")
|
||||
return self
|
||||
|
||||
def predict(self, X):
|
||||
# Return dummy predictions for testing
|
||||
batch_size = X.shape[0] if hasattr(X, 'shape') else 1
|
||||
if self.output_size == 1:
|
||||
pred_class = np.random.choice([0, 1], size=batch_size)
|
||||
pred_proba = np.random.random(batch_size)
|
||||
else:
|
||||
pred_class = np.random.randint(0, self.output_size, size=batch_size)
|
||||
pred_proba = np.random.random((batch_size, self.output_size))
|
||||
logger.debug(f"Fallback CNN prediction: class={pred_class}, proba_shape={np.array(pred_proba).shape}")
|
||||
return pred_class, pred_proba
|
||||
self.model.eval()
|
||||
with torch.no_grad():
|
||||
if isinstance(X, np.ndarray):
|
||||
X = torch.FloatTensor(X)
|
||||
if len(X.shape) == 2: # Add batch dimension if needed
|
||||
X = X.unsqueeze(0)
|
||||
|
||||
outputs = self.model(X)
|
||||
probs = F.softmax(outputs, dim=1)
|
||||
pred_class = torch.argmax(probs, dim=1).numpy()
|
||||
pred_proba = probs.numpy()
|
||||
|
||||
logger.debug(f"Fallback CNN prediction: class={pred_class}, proba_shape={pred_proba.shape}")
|
||||
return pred_class, pred_proba
|
||||
|
||||
def fit(self, X, y, **kwargs):
|
||||
logger.info(f"Fallback CNN training: X_shape={X.shape}, y_shape={y.shape}")
|
||||
self.model.train()
|
||||
if isinstance(X, np.ndarray):
|
||||
X = torch.FloatTensor(X)
|
||||
if isinstance(y, np.ndarray):
|
||||
y = torch.LongTensor(y)
|
||||
if len(X.shape) == 2: # Add batch dimension if needed
|
||||
X = X.unsqueeze(0)
|
||||
if len(y.shape) == 1: # Add batch dimension if needed
|
||||
y = y.unsqueeze(0)
|
||||
|
||||
self.optimizer.zero_grad()
|
||||
outputs = self.model(X)
|
||||
loss = self.criterion(outputs, y)
|
||||
loss.backward()
|
||||
self.optimizer.step()
|
||||
|
||||
logger.info(f"Fallback CNN training: X_shape={X.shape}, y_shape={y.shape}, loss={loss.item():.4f}")
|
||||
return self
|
||||
|
||||
logger.warning("Using fallback CNN model - CNN training will work but with dummy predictions")
|
||||
logger.warning("Using fallback CNN model - CNN training will work with PyTorch implementation")
|
||||
|
||||
try:
|
||||
from core.unified_data_stream import TrainingDataPacket
|
||||
|
Reference in New Issue
Block a user