Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving PyTorch model
#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


Messages In This Thread
Saving PyTorch model - by kiton - Nov-23-2018, 04:52 PM
RE: Saving PyTorch model - by Larz60+ - Nov-23-2018, 05:11 PM
RE: Saving PyTorch model - by kiton - Nov-23-2018, 05:22 PM
RE: Saving PyTorch model - by kiton - Nov-24-2018, 01:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyTorch for beginners OmegaRed94 1 1,848 Jun-09-2022, 09:20 PM
Last Post: Larz60+
  PyTorch GTX 770 OLD ? samuelbachorik 0 2,719 Jan-24-2021, 05:42 PM
Last Post: samuelbachorik
  [PyTorch] no CUDA-capable device is detected constantin01 0 3,914 Apr-17-2020, 05:50 AM
Last Post: constantin01
  Free ebook "Deep Learning with PyTorch" ThomasL 0 2,863 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