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
文章来源地址https://www.toymoban.com/news/detail-837438.html
文章来源:https://www.toymoban.com/news/detail-837438.html
到了这里,关于deeplearning with pytorch(二)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!