hummingbird icon indicating copy to clipboard operation
hummingbird copied to clipboard

Better error message for converting before fit

Open ksaur opened this issue 4 years ago • 0 comments

If a user forgets to call fit before calling convert they will get a confusing error message. We should find a way to print a more helpful error message in a case such as a the following:

>>> import numpy as np
>>> from sklearn.linear_model  import LinearRegression
>>> from hummingbird.ml import convert

>>> X = np.random.rand(100, 200)
>>> X = np.array(X, dtype=np.float32)
>>> y = np.random.randint(2, size=100)

>>> l = LinearRegression( fit_intercept=True, normalize=False, copy_X=True, n_jobs=None   )

>>> l_hb = convert(l, 'torch')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/hummingbird/ml/convert.py", line 252, in convert
    return _convert_sklearn(model, backend, test_input, device, extra_config)
  File "/usr/local/lib/python3.7/site-packages/hummingbird/ml/convert.py", line 80, in _convert_sklearn
    hb_model = topology_converter(topology, backend, device, extra_config=extra_config)
  File "/usr/local/lib/python3.7/site-packages/hummingbird/ml/_topology.py", line 76, in convert
    raise e
  File "/usr/local/lib/python3.7/site-packages/hummingbird/ml/_topology.py", line 68, in convert
    operator_map[operator.full_name] = converter(operator, device, extra_config)
  File "/usr/local/lib/python3.7/site-packages/hummingbird/ml/operator_converters/sklearn/linear.py", line 66, in convert_sklearn_linear_regression_model
    coefficients = operator.raw_operator.coef_.transpose().reshape(-1, 1).astype("float32")
AttributeError: 'LinearRegression' object has no attribute 'coef_'

>>> l.fit(X,y)
LinearRegression()
>>> l_hb = convert(l, 'torch')
>>> l_hb
<hummingbird.ml._container.PyTorchSklearnContainerRegression object at 0x137ce2610>

ksaur avatar Oct 04 '20 22:10 ksaur