automl / Auto-PyTorch

Automatic architecture search and hyperparameter optimization for PyTorch
Apache License 2.0
2.37k stars 287 forks source link

autonet.get_pytorch_model() function return a network different from autonet itself #21

Closed zym604 closed 4 years ago

zym604 commented 4 years ago

Hi, I find that the autonet prediction results are different from the result predicted by the pytorch model obtained from autonet using get_pytorch_model function. To be specific: res_autonet = autonet.predict(X_train) is different from:

model = autonet.get_pytorch_model()
model.eval()
X_train = X_train.astype(np.float32)
res_model = model(Variable(torch.from_numpy(X_train).cuda())).data.cpu().numpy()

image As can be seen in the third picture, the two predictions have different values, while they're supposed to be equal.

The full code could be seen as attached:

__author__ = "Max Dippel, Michael Burkart and Matthias Urban"
__version__ = "0.0.1"
__license__ = "BSD"

import os, sys
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..", "..")))
from autoPyTorch import AutoNetRegression
from autoPyTorch.data_management.data_manager import DataManager

# Note: You can write your own datamanager! Call fit train, valid data (numpy matrices) 
dm = DataManager()
dm.generate_regression(num_features=21, num_samples=1500)
X_train=dm.X
Y_train=dm.Y
X_valid=dm.X_train
Y_valid=dm.Y_train

# Note: every parameter has a default value, you do not have to specify anything. The given parameter allow a fast test.
autonet = AutoNetRegression(budget_type='epochs', min_budget=1, max_budget=9, num_iterations=1, log_level='info')

res = autonet.fit(X_train=X_train, Y_train=Y_train, X_valid=X_valid, Y_valid=Y_valid)

print(res)

res_autonet = autonet.predict(X_train)

##retrain
import numpy as np
import torch
from torch.autograd import Variable
model = autonet.get_pytorch_model()
model.eval()
#autonet.print_help()
X_train = X_train.astype(np.float32)
res_model = model(Variable(torch.from_numpy(X_train).cuda())).data.cpu().numpy()

#plot
import matplotlib.pyplot as plt
plt.figure(figsize=(10,7))
plt.subplot(221)
plt.plot(Y_train,res_model,'.')
plt.xlabel('True result')
plt.ylabel('Model result')
plt.subplot(222)
plt.plot(Y_train,res_autonet,'.')
plt.xlabel('True result')
plt.ylabel('Model result')
plt.subplot(223)
plt.plot(res_autonet,res_model,'.')
plt.xlabel('AutoNet result')
plt.ylabel('Model result')
zym604 commented 4 years ago

I ask this question because the degree of difference is much larger in my case using my own data: image So I return to check the example code and find out that such a difference exists in it too. It's just the degree of difference not so much as my case.

LMZimmer commented 4 years ago

Note that the autonet pipeline also includes all data preprocessing such as normalization. Did you consider that?

zym604 commented 4 years ago

Note that the autonet pipeline also includes all data preprocessing such as normalization. Did you consider that?

I didn't, that's possible the reason.