Import pytorch and use hdl coder to create system C code
Show older comments
I created a deep learning mode on using pytorch using the following script
class RegressionANN(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(8, 64) # Increased neurons to 64
self.linear2 = nn.Linear(64, 1) # Changed input to 64
def forward(self, x):
x = torch.relu(self.linear1(x))
x = self.linear2(x)
return x
model = RegressionANN()
loss_fn = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
# Forward pass
y_pred = model(X_train_tensor)
# Calculate initial loss
initial_loss = loss_fn(y_pred, y_train_tensor)
print(f"Initial Loss: {initial_loss.item()}")
epochs = 100
for epoch in range(epochs):
# Zero gradients
optimizer.zero_grad()
# Forward pass
y_pred = model(X_train_tensor)
# Compute loss
loss = loss_fn(y_pred, y_train_tensor)
# Backpropagation
loss.backward()
# Update parameters
optimizer.step()
if (epoch + 1) % 10 == 0:
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')
print(f"Loss after {epochs} iterations: {loss.item():.4f}")
Following that I did the following
X = torch.rand(1, 8)
traced_model = torch.jit.trace(model.forward, X)
traced_model.save("traced_linear_model.pt")
Model was saved successfully. I tried importing the model using
importNetworkFromPyTorch
How do i proceed with HDL code generation ?
Importing created a matlab .m file for each layer.
Which matlab file should I use for code generation ?
Accepted Answer
More Answers (0)
Categories
Find more on Code Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!