-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
45 lines (40 loc) · 1.63 KB
/
model.py
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
# Import necessary libraries from PyTorch
import torch
import torch.nn as nn
# Define a neural network class by inheriting from nn.Module
class NeuralNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
"""
Initialize the neural network layers.
Parameters:
input_size (int): The number of input features.
hidden_size (int): The number of neurons in the hidden layers.
num_classes (int): The number of output classes.
"""
super(NeuralNet, self).__init__()
# Define the first linear layer (input to hidden)
self.l1 = nn.Linear(input_size, hidden_size)
# Define the second linear layer (hidden to hidden)
self.l2 = nn.Linear(hidden_size, hidden_size)
# Define the third linear layer (hidden to output)
self.l3 = nn.Linear(hidden_size, num_classes)
# Define the ReLU activation function
self.relu = nn.ReLU()
def forward(self, x):
"""
Perform a forward pass through the network.
Parameters:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output of the neural network.
"""
# Pass the input through the first layer and apply ReLU activation
out = self.l1(x)
out = self.relu(out)
# Pass the result through the second layer and apply ReLU activation
out = self.l2(out)
out = self.relu(out)
# Pass the result through the third layer (output layer)
out = self.l3(out)
# No activation function or softmax is applied to the output
return out