Slackbot
06/29/2023, 1:02 AMlarme (shenyang)
06/29/2023, 4:50 AMTaeef Najib
06/29/2023, 9:14 PMtorch.FloatTensor(X_train) and torch.LongTensor(y_train) as input. While testing it takes a NumpyNdarray such as [[10,168,74,0,0,38,0.537,34]] as input and is expected to return the class 0 or 1 as the output.
Here's a portion of my code if that helps:
# Creating ANN model with PyTorch
class ANN_Model(nn.Module):
def __init__(self,input_features=8,hidden1=20,hidden2=20,out_features=2):
super().__init__()
self.f_connected1=nn.Linear(input_features,hidden1)
self.f_connected2=nn.Linear(hidden1,hidden2)
self.out=nn.Linear(hidden2,out_features)
def forward(self,x):
x=F.relu(self.f_connected1(x))
x=F.relu(self.f_connected2(x))
x=self.out(x)
return x
# Training model
def train(hp: Hyperparameters, X_train: torch.FloatTensor, y_train: torch.LongTensor) -> nn.Module:
torch.manual_seed(hp.random_state)
model=ANN_Model()
# Defining the loss function and the optimizer
loss_function=nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(model.parameters(),lr=<http://hp.lr|hp.lr>)
final_losses=[]
for i in range(hp.epochs):
i=i+1
y_pred=model.forward(X_train)
loss=loss_function(y_pred,y_train)
final_losses.append(loss)
if i%10==1:
print(f"Epoch: {i} Loss: {loss.item()}")
optimizer.zero_grad()
loss.backward()
optimizer.step()
return model
Please let me know if you need more info about my code.larme (shenyang)
06/30/2023, 6:37 PMbentoml.pytorch.save(
model,
"my_torch_model",
signatures={"__call__": {"batchable": True, "batch_dim": 0}},
)
Then the input of model_runner.async_run should be a batch input. So I'd like to suggest expand_dim the input_tensor at axis=0 like:
inp = np.expand_dims(inp, axis=0)
input_tensor = torch.FloatTensor(inp)
...
and see if this fix the issueTaeef Najib
07/01/2023, 8:18 AMsignatures = {"__call__"" {"batchable": True, "batch_dim":0}}, but i tested with inp = np.expand_dim(inp, axis=0) and it returned [[[-11.270812034606934, -9.508756637573242]]] now. Previously it returned [[-11.270812034606934, -9.508756637573242]]
By any chance, can I use this in service.py to return 0 or 1?
import numpy as np
output = [[-11.270812034606934, -9.508756637573242]]
prediction = 0 if output[0][0] > output[0][1] else 1
print(prediction)larme (shenyang)
07/03/2023, 7:49 PMTaeef Najib
07/04/2023, 2:07 AM