0
点赞
收藏
分享

微信扫一扫

Ubuntu Docker Cuda 环境安装记录

微言记 03-05 15:30 阅读 4

3.Evaluate Test Data Set On Network

# evaluate Model on test dataset(validate model on test set)
with torch.no_grad(): #basically turn off back propogation
    y_eval = model.forward(X_test) # X-test are features from our test set,y_eval will be predictions
    loss = criterion(y_eval, y_test) #find the loss or error
    #print(loss)

corret = 0
with torch.no_grad(): 
    for i, data in enumerate(X_test):
        y_val = model.forward(data)

        # if y_test[i] == 0:
        #     x = 'setosa'
        # elif y_test[i] == 1:
        #     x = 'versicolor'
        # else:
        #     x = 'virginica'

        
        # will tell us what typ of flower class out network think it is
        print(f'{i + 1}. {str(y_val)} \t y_test:{y_test[i]} \t y_val: { y_val.argmax().item()}')

        #correct or not
        if y_val.argmax().item() == y_test[i]:
            corret += 1
    
    print(f'we got {corret} correct')

4. Evaluate new data on the network

new_iris = torch.tensor([4.7, 3.2, 1.3, 0.2])

with torch.no_grad():
    # print(model.forward(new_iris))
    print(model(new_iris))

 5.save and load out nerual network model for pytorch and python

#save our NN model
torch.save(model.state_dict(), 'my_iris_model.pt')
# load the saved model
new_model = Model()
new_model.load_state_dict(torch.load('my_iris_model.pt'))
# make sure it loaded correctly
new_model.eval() #Set the module in evaluation mode

 

举报

相关推荐

0 条评论