3

So, I basically wanted to make use of PyTorch on ECL, through python embedding.When we constructed the architecture of a simple model by creating a class as shown below, we observed a few unusual errors like the class name not being defined:

f() := EMBED(PY)
import torch
import torch.nn as nn
import torch.optim as optim
class model_1(nn.Module):
 def _init_(self):
 super(model_1, self)._init_()
 input_size = torch.randint(10, 100, (1,)).item()
 hidden_size = torch.randint(10, 100, (1,)).item()
 output_size = torch.randint(2, 10, (1,)).item()
 self.layer1 = nn.Linear(input_size, hidden_size)
 self.activation = nn.ReLU()
 self.layer2 = nn.Linear(hidden_size, output_size)
 self.softmax = nn.Softmax(dim=1)
 
 def forward(self, x):
 x = self.layer1(x)
 x = self.activation(x)
 x = self.layer2(x)
 x = self.softmax(x)
 return x
model = model_1()
batch_size = 32
input_size = model.layer1.in_features
input_data = torch.randn(batch_size, input_size)
target = torch.randint(0, model.layer2.out_features, (batch_size,))
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
num_epochs = 10
for epoch in range(num_epochs):
 output = model(input_data)
 
 loss = criterion(output, target)
 
 
 print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
 
 optimizer.zero_grad()
 loss.backward()
 optimizer.step()
ENDEMBED;
f();

We were getting the following error:

Error: 0: pyembed: name 'model_1' is not defined

We also tried to run the above model without classes just by using functions, and this is what we are getting:

f() := EMBED(PY)
import torch
import torch.nn as nn
import torch.optim as optim
def create_model():
 input_size = torch.randint(10, 100, (1,)).item()
 hidden_size = torch.randint(10, 100, (1,)).item()
 output_size = torch.randint(2, 10, (1,)).item()
 
 layer1 = nn.Linear(input_size, hidden_size)
 activation = nn.ReLU()
 layer2 = nn.Linear(hidden_size, output_size)
 softmax = nn.Softmax(dim=1)
 
 def forward(x):
 x = layer1(x)
 x = activation(x)
 x = layer2(x)
 x = softmax(x)
 return x
 
 
 parameters = list(layer1.parameters()) + list(layer2.parameters())
 
 return forward, parameters
model, parameters = create_model()
batch_size = 32
input_size = parameters[0].size(1) # Input size of the first layer
input_data = torch.randn(batch_size, input_size)
target = torch.randint(0, parameters[-1].size(0), (batch_size,)) 
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(parameters, lr=0.01)
num_epochs = 10
for epoch in range(num_epochs):
 output = model(input_data)
 
 loss = criterion(output, target)
 
 print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
 
 optimizer.zero_grad()
 loss.backward()
 optimizer.step()
ENDEMBED;
f();

On running this, we were getting the following error (Although torch is already configured, I ran an import torch in a simple python program, didn't give me an error):

Error: 0: pyembed: name 'torch' is not defined

How can I create an object of the mentioned class above using python embedding?

asked May 11, 2024 at 10:06

1 Answer 1

1

You need to have a common global area shared between calls. Your class definition is out of scope when you are trying to use it. You also need to define a persist setting. It’s all in the following blog:

Advanced Python Embedding

Here is another blog you may find helpful:

Power Macros in Python

Thanks for your question!

answered May 14, 2024 at 15:45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.