-
How can i deploy a logistic regression model written in PyTorch? The model's arch is: import torch
import torch.nn as nn
import torch.nn.functional as F
class LogisticRegression(nn.Module):
def __init__(self, input_len_data=55):
super(LogisticRegression,self).__init__()
self.f1 = nn.Linear(input_len_data, 1000)
self.f2 = nn.Linear(1000, 1)
def forward(self,x):
x = self.f1(x)
x = F.leaky_relu(x)
x = F.dropout(x, p = 0.2)
x = self.f2(x)
return F.sigmoid(x)
def predict(self, inp):
"""predict digit for input"""
self.eval()
with torch.inference_mode():
raw_output = self(inp)
pred = torch.round(raw_output)
return pred I trained the model with this script: device = "cuda" if torch.cuda.is_available() else "cpu"
model = LogisticRegression(input_len_data=55).to(device=device)
learning_rate = 0.0001
criterion = torch.nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# Calculate accuracy (a classification metric)
def accuracy_fn(y_true, y_pred):
correct = torch.eq(y_true, y_pred).sum().item() # torch.eq() calculates where two tensors are equal
acc = (correct / len(y_pred)) * 100
return acc
# Fit the model
torch.manual_seed(42)
epochs = 1000
for epoch in range(epochs):
### Training
model.train()
# 1. Forward pass
y_pred_train = model(X_train).squeeze() # squeeze removes dimensions of size 1
# 2. Calculate loss/accuracy
loss = criterion(y_pred_train, y_train)
acc = (torch.eq(torch.round(y_pred_train), y_train).sum().item() / len(y_train)) * 100
# 3. Optimizer zero grad
optimizer.zero_grad()
# 4. Loss backwards
loss.backward()
# 5. Optimizer step
optimizer.step()
### Testing
model.eval()
with torch.inference_mode():
# 1. Forward pass
y_pred_test = torch.round(model(X_test).squeeze())
# 2. Caculate loss/accuracy
test_loss = criterion(y_pred_test,
y_test)
test_acc = (torch.eq(torch.round(y_pred_test), y_test).sum().item() / len(y_test)) * 100
# Print out what's happening every 10 epochs
if epoch % 100 == 0:
print(f"Epoch: {epoch} | Loss: {loss:.5f}, Accuracy: {acc:.2f}% | Test loss: {test_loss:.5f}, Test acc: {test_acc:.2f}%")
#print(f"Predictions: {y_pred_test}/n") And saved the model in a bento with this command: bentoml.pytorch.save_model('logits_V1_1_bento', model) To serve the bento i used this service script: import numpy as np
import bentoml
from bentoml.io import NumpyNdarray
import torch
model_runner = bentoml.pytorch.get("logits_v1_1_bento:gbrwqwwm2oqhx3p3").to_runner()
svc = bentoml.Service(name="test_service", runners=[model_runner])
def to_numpy(tensor):
return tensor.detach().cpu().numpy()
@svc.api(
input=NumpyNdarray(),
output=NumpyNdarray()
)
async def classify(input_series) -> np.ndarray:
#assert input_series.shape == (1, 53)
input_tensor = torch.from_numpy(input_series)
#result = model_runner.predict.run(input_tensor)
result = await model_runner.async_run(input_tensor)
return to_numpy(result)
# bentoml serve service:svc The serving is working but when i make a request with a csv file curl -X POST "http://127.0.0.1:3000/classify" -T ./torchserve_test.csv -H "Content-type: text/csv" I receive this error:
How can i resolve this error? |
Beta Was this translation helpful? Give feedback.
Answered by
IonBoleac
Feb 17, 2024
Replies: 1 comment
-
The problem is resolved. I fixed adding the data that is send in json format. The solution is arrived meanwhile i take a shower ahahah |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
IonBoleac
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is resolved. I fixed adding the data that is send in json format. The solution is arrived meanwhile i take a shower ahahah