koaning / embetter

just a bunch of useful embeddings
https://koaning.github.io/embetter/
MIT License
465 stars 15 forks source link

'SentenceEncoder' object has no attribute 'device' #36

Closed nicholas-dinicola closed 1 year ago

nicholas-dinicola commented 1 year ago
text_emb_pipeline = make_pipeline(
  ColumnGrabber("text"),
  SentenceEncoder('all-MiniLM-L6-v2')
)

# This pipeline can also be trained to make predictions, using
# the embedded features. 
text_clf_pipeline = make_pipeline(
  text_emb_pipeline,
  LogisticRegression()
)

dataf = pd.DataFrame({
  "text": ["positive sentiment", "super negative"],
  "label_col": ["pos", "neg"]
})

X = text_emb_pipeline.fit_transform(dataf, dataf['label_col'])
text_clf_pipeline.fit(dataf, dataf['label_col'])

This code gives this error: 'SentenceEncoder' object has no attribute 'device'

koaning commented 1 year ago

That's strange. Could you share the version embetter?

koaning commented 1 year ago

Also, could you share the full error?

nicholas-dinicola commented 1 year ago

embitter version: embetter 0.2.3

nicholas-dinicola commented 1 year ago

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/miniconda3/lib/python3.9/site-packages/IPython/core/formatters.py:973, in MimeBundleFormatter.__call__(self, obj, include, exclude)
    970     method = get_real_method(obj, self.print_method)
    972     if method is not None:
--> 973         return method(include=include, exclude=exclude)
    974     return None
    975 else:

File ~/miniconda3/lib/python3.9/site-packages/sklearn/base.py:601, in BaseEstimator._repr_mimebundle_(self, **kwargs)
    599 def _repr_mimebundle_(self, **kwargs):
    600     """Mime bundle used by jupyter kernels to display estimator"""
--> 601     output = {"text/plain": repr(self)}
    602     if get_config()["display"] == "diagram":
    603         output["text/html"] = estimator_html_repr(self)

File ~/miniconda3/lib/python3.9/site-packages/sklearn/base.py:238, in BaseEstimator.__repr__(self, N_CHAR_MAX)
    230 # use ellipsis for sequences with a lot of elements
    231 pp = _EstimatorPrettyPrinter(
    232     compact=True,
    233     indent=1,
    234     indent_at_name=True,
    235     n_max_elements_to_show=N_MAX_ELEMENTS_TO_SHOW,
    236 )
--> 238 repr_ = pp.pformat(self)
...
--> 170     value = getattr(self, key)
    171     if deep and hasattr(value, "get_params") and not isinstance(value, type):
    172         deep_items = value.get_params().items()

AttributeError: 'SentenceEncoder' object has no attribute 'device'
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/miniconda3/lib/python3.9/site-packages/IPython/core/formatters.py:707, in PlainTextFormatter.__call__(self, obj)
    700 stream = StringIO()
    701 printer = pretty.RepresentationPrinter(stream, self.verbose,
    702     self.max_width, self.newline,
    703     max_seq_length=self.max_seq_length,
    704     singleton_pprinters=self.singleton_printers,
    705     type_pprinters=self.type_printers,
    706     deferred_pprinters=self.deferred_printers)
--> 707 printer.pretty(obj)
    708 printer.flush()
    709 return stream.getvalue()

File ~/miniconda3/lib/python3.9/site-packages/IPython/lib/pretty.py:410, in RepresentationPrinter.pretty(self, obj)
    407                         return meth(obj, self, cycle)
    408                 if cls is not object \
    409                         and callable(cls.__dict__.get('__repr__')):
--> 410                     return _repr_pprint(obj, self, cycle)
    412     return _default_pprint(obj, self, cycle)
    413 finally:

File ~/miniconda3/lib/python3.9/site-packages/IPython/lib/pretty.py:778, in _repr_pprint(obj, p, cycle)
    776 """A pprint that just redirects to the normal repr function."""
    777 # Find newlines and replace them with p.break_()
...
--> 170     value = getattr(self, key)
    171     if deep and hasattr(value, "get_params") and not isinstance(value, type):
    172         deep_items = value.get_params().items()

AttributeError: 'SentenceEncoder' object has no attribute 'device'
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/miniconda3/lib/python3.9/site-packages/IPython/core/formatters.py:343, in BaseFormatter.__call__(self, obj)
    341     method = get_real_method(obj, self.print_method)
    342     if method is not None:
--> 343         return method()
    344     return None
    345 else:

File ~/miniconda3/lib/python3.9/site-packages/sklearn/base.py:597, in BaseEstimator._repr_html_inner(self)
    592 def _repr_html_inner(self):
    593     """This function is returned by the @property `_repr_html_` to make
    594     `hasattr(estimator, "_repr_html_") return `True` or `False` depending
    595     on `get_config()["display"]`.
    596     """
--> 597     return estimator_html_repr(self)

File ~/miniconda3/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:393, in estimator_html_repr(estimator)
    391 style_template = Template(_STYLE)
    392 style_with_id = style_template.substitute(id=container_id)
--> 393 estimator_str = str(estimator)
    395 # The fallback message is shown by default and loading the CSS sets
    396 # div.sk-text-repr-fallback to display: none to hide the fallback message.
    397 #
   (...)
...
--> 170     value = getattr(self, key)
    171     if deep and hasattr(value, "get_params") and not isinstance(value, type):
    172         deep_items = value.get_params().items()

AttributeError: 'SentenceEncoder' object has no attribute 'device'
koaning commented 1 year ago

I just ran a fresh install on Colab and did not get this issue.

Could you try from a fresh venv and try again?

koaning commented 1 year ago

It's also a bit strange since there are tests for the code in the README file.

koaning commented 1 year ago

Closing due to radio silence, feel free to re-open though.

nicholas-dinicola commented 1 year ago

Sorry for the delay in replying. I managed to solve the issue creating a new virtual environment from scratch and by running pip install embetter[sentence-tfm]. The error was probably due to some conflicts with other dependencies. Thanks for helping though.