Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving PyTorch model
#1
Hello dear forum members,

I am following the example of binary classification problem provided by Numerai competition (https://www.kaggle.com/solomonk/pytorch-...sification). I was able to successfully run the code. My question is how to save (and then load) the trained model from that example? I am having an issue understanding where exactly the model to be saved is in this code.

Thank you!
Reply
#2
Do you know any python?
Is this an assignment?
You will need to open a file for output,example:
with open('myfile.suffix', 'w') as fp:
then indent all code so it's part of the file loop, or just open and then close at end.
in either case, you will write output data to the file wherever you want using something similar to:
    fp.write(somedata)
Reply
#3
Larz60+ Thank you for response. My knowledge of python is limited. No, this is not an assignment. I have no problem saving the resulting data into the CSV. What I am struggling with is saving a PyTorch trained model itself.

Some sources suggest:
torch.save(the_model.state_dict(), PATH)
but what I cannot figure out from that code is where exactly the model to be saved is. For example, here is the part of the code for training, where is model here?
import time
start_time = time.time()    
epochs=60 # change to 1500 for better results
all_losses = []

X_tensor_train= XnumpyToTensor(trainX)
Y_tensor_train= YnumpyToTensor(trainY)

print(type(X_tensor_train.data), type(Y_tensor_train.data)) # should be 'torch.cuda.FloatTensor'

# From here onwards, we must only use PyTorch Tensors
for step in range(epochs):    
    out = net(X_tensor_train)                 # input x and predict based on x
    cost = loss_func(out, Y_tensor_train)     # must be (1. nn output, 2. target), the target label is NOT one-hotted

    optimizer.zero_grad()   # clear gradients for next train
    cost.backward()         # backpropagation, compute gradients
    optimizer.step()        # apply gradients
                   
        
    if step % 5 == 0:        
        loss = cost.data[0]
        all_losses.append(loss)
        print(step, cost.data.cpu().numpy())
        # RuntimeError: can't convert CUDA tensor to numpy (it doesn't support GPU arrays). 
        # Use .cpu() to move the tensor to host memory first.        
        prediction = (net(X_tensor_train).data).float() # probabilities         
#         prediction = (net(X_tensor).data > 0.5).float() # zero or one
#         print ("Pred:" + str (prediction)) # Pred:Variable containing: 0 or 1
#         pred_y = prediction.data.numpy().squeeze()            
        pred_y = prediction.cpu().numpy().squeeze()
        target_y = Y_tensor_train.cpu().data.numpy()
                        
        tu = (log_loss(target_y, pred_y),roc_auc_score(target_y,pred_y ))
        print ('LOG_LOSS={}, ROC_AUC={} '.format(*tu))        
                
end_time = time.time()
print ('{} {:6.3f} seconds'.format('GPU:', end_time-start_time))

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(all_losses)
plt.show()

false_positive_rate, true_positive_rate, thresholds = roc_curve(target_y,pred_y)
roc_auc = auc(false_positive_rate, true_positive_rate)

plt.title('LOG_LOSS=' + str(log_loss(target_y, pred_y)))
plt.plot(false_positive_rate, true_positive_rate, 'b', label='AUC = %0.6f' % roc_auc)
plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([-0.1, 1.2])
plt.ylim([-0.1, 1.2])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
Reply
#4
It appears that this particular example cannot in fact be saved as a trained model. Here one person states "As far as I know, we cannot save a sklearn wrapped keras model. We must use the Keras API directly to save/load the model."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyTorch for beginners OmegaRed94 1 1,092 Jun-09-2022, 09:20 PM
Last Post: Larz60+
  PyTorch GTX 770 OLD ? samuelbachorik 0 1,919 Jan-24-2021, 05:42 PM
Last Post: samuelbachorik
  [PyTorch] no CUDA-capable device is detected constantin01 0 3,308 Apr-17-2020, 05:50 AM
Last Post: constantin01
  Free ebook "Deep Learning with PyTorch" ThomasL 0 2,337 Nov-22-2019, 02:50 PM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020