File size: 2,661 Bytes
9dce563
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
import numpy as np

# πŸ”₯ Deep Neural Network Model
class DeepNeuralNetwork(nn.Module):
    def __init__(self, input_size, hidden_sizes, output_size):
        super(DeepNeuralNetwork, self).__init__()
        layers = []
        in_size = input_size

        for hidden_size in hidden_sizes:
            layers.append(nn.Linear(in_size, hidden_size))
            layers.append(nn.ReLU())
            in_size = hidden_size

        layers.append(nn.Linear(in_size, output_size))
        self.model = nn.Sequential(*layers)

    def forward(self, x):
        return self.model(x)  # βœ… Apply the model layers properly


# πŸ”₯ Training Function
def train_model(model, criterion, optimizer, x_train, y_train, epochs=100):
    model.train()
    for epoch in range(epochs):
        optimizer.zero_grad()

        # Forward pass
        y_pred = model(x_train)

        # Loss calculation
        loss = criterion(y_pred, y_train)

        # Backward pass
        loss.backward()
        optimizer.step()

        if (epoch + 1) % 10 == 0:
            print(f'Epoch [{epoch + 1}/{epochs}], Loss: {loss.item():.4f}')


# βœ… Example Usage
if __name__ == "__main__":
    # πŸ”₯ Sample Data
    x_train = torch.randn(100, 10, requires_grad=True)  # βœ… Require gradient tracking
    y_train = torch.randint(0, 2, (100,), dtype=torch.long)  # βœ… Ensure LongTensor for CrossEntropyLoss

    # Plotting the input data
    plt.scatter(x_train[:, 0].detach().numpy(), x_train[:, 1].detach().numpy(), c=y_train.numpy(), cmap='viridis')
    plt.title('Deep Neural Network Input Data')
    plt.xlabel('Input Feature 1')
    plt.ylabel('Input Feature 2')
    plt.colorbar(label='Output Class')
    plt.show()

    # Initialize Model
    model = DeepNeuralNetwork(input_size=10, hidden_sizes=[20, 10], output_size=2)

    # Criterion and Optimizer
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.parameters(), lr=0.01)

    # Train the model
    train_model(model, criterion, optimizer, x_train, y_train, epochs=100)

    # βœ… Plotting the predictions with softmax
    model.eval()
    with torch.no_grad():
        y_pred = torch.softmax(model(x_train), dim=1).detach().numpy()

    plt.scatter(x_train[:, 0].detach().numpy(), x_train[:, 1].detach().numpy(), c=np.argmax(y_pred, axis=1), cmap='viridis')
    plt.title('Deep Neural Network Predictions')
    plt.xlabel('Input Feature 1')
    plt.ylabel('Input Feature 2')
    plt.colorbar(label='Predicted Class')
    plt.show()