Sentence Similarity
sentence-transformers
Safetensors
code
mpnet
feature-extraction
Generated from Trainer
dataset_size:20000
loss:CoSENTLoss
loss:MultipleNegativesRankingLoss
Eval Results (legacy)
text-embeddings-inference
Instructions to use BoghdadyJR/al-MiniLM-L6-v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use BoghdadyJR/al-MiniLM-L6-v2 with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("BoghdadyJR/al-MiniLM-L6-v2") sentences = [ "KeypointsOnImage.to_xy_array", "def to_xy_array(self):\n \"\"\"\n Convert keypoint coordinates to ``(N,2)`` array.\n\n Returns\n -------\n (N, 2) ndarray\n Array containing the coordinates of all keypoints.\n Shape is ``(N,2)`` with coordinates in xy-form.\n\n \"\"\"\n result = np.zeros((len(self.keypoints), 2), dtype=np.float32)\n for i, keypoint in enumerate(self.keypoints):\n result[i, 0] = keypoint.x\n result[i, 1] = keypoint.y\n return result", "def _generateMetricSpecs(options):\n \"\"\" Generates the Metrics for a given InferenceType\n\n Parameters:\n -------------------------------------------------------------------------\n options: ExpGenerator options\n retval: (metricsList, optimizeMetricLabel)\n metricsList: list of metric string names\n optimizeMetricLabel: Name of the metric which to optimize over\n\n \"\"\"\n inferenceType = options['inferenceType']\n inferenceArgs = options['inferenceArgs']\n predictionSteps = inferenceArgs['predictionSteps']\n metricWindow = options['metricWindow']\n if metricWindow is None:\n metricWindow = int(Configuration.get(\"nupic.opf.metricWindow\"))\n\n metricSpecStrings = []\n optimizeMetricLabel = \"\"\n\n # -----------------------------------------------------------------------\n # Generate the metrics specified by the expGenerator paramters\n metricSpecStrings.extend(_generateExtraMetricSpecs(options))\n\n # -----------------------------------------------------------------------\n\n optimizeMetricSpec = None\n # If using a dynamically computed prediction steps (i.e. when swarming\n # over aggregation is requested), then we will plug in the variable\n # predictionSteps in place of the statically provided predictionSteps\n # from the JSON description.\n if options['dynamicPredictionSteps']:\n assert len(predictionSteps) == 1\n predictionSteps = ['$REPLACE_ME']\n\n # -----------------------------------------------------------------------\n # Metrics for temporal prediction\n if inferenceType in (InferenceType.TemporalNextStep,\n InferenceType.TemporalAnomaly,\n InferenceType.TemporalMultiStep,\n InferenceType.NontemporalMultiStep,\n InferenceType.NontemporalClassification,\n 'MultiStep'):\n\n predictedFieldName, predictedFieldType = _getPredictedField(options)\n isCategory = _isCategory(predictedFieldType)\n metricNames = ('avg_err',) if isCategory else ('aae', 'altMAPE')\n trivialErrorMetric = 'avg_err' if isCategory else 'altMAPE'\n oneGramErrorMetric = 'avg_err' if isCategory else 'altMAPE'\n movingAverageBaselineName = 'moving_mode' if isCategory else 'moving_mean'\n\n # Multi-step metrics\n for metricName in metricNames:\n metricSpec, metricLabel = \\\n _generateMetricSpecString(field=predictedFieldName,\n inferenceElement=InferenceElement.multiStepBestPredictions,\n metric='multiStep',\n params={'errorMetric': metricName,\n 'window':metricWindow,\n 'steps': predictionSteps},\n returnLabel=True)\n metricSpecStrings.append(metricSpec)\n\n # If the custom error metric was specified, add that\n if options[\"customErrorMetric\"] is not None :\n metricParams = dict(options[\"customErrorMetric\"])\n metricParams['errorMetric'] = 'custom_error_metric'\n metricParams['steps'] = predictionSteps\n # If errorWindow is not specified, make it equal to the default window\n if not \"errorWindow\" in metricParams:\n metricParams[\"errorWindow\"] = metricWindow\n metricSpec, metricLabel =_generateMetricSpecString(field=predictedFieldName,\n inferenceElement=InferenceElement.multiStepPredictions,\n metric=\"multiStep\",\n params=metricParams,\n returnLabel=True)\n metricSpecStrings.append(metricSpec)\n\n # If this is the first specified step size, optimize for it. Be sure to\n # escape special characters since this is a regular expression\n optimizeMetricSpec = metricSpec\n metricLabel = metricLabel.replace('[', '\\\\[')\n metricLabel = metricLabel.replace(']', '\\\\]')\n optimizeMetricLabel = metricLabel\n\n if options[\"customErrorMetric\"] is not None :\n optimizeMetricLabel = \".*custom_error_metric.*\"\n\n # Add in the trivial metrics\n if options[\"runBaselines\"] \\\n and inferenceType != InferenceType.NontemporalClassification:\n for steps in predictionSteps:\n metricSpecStrings.append(\n _generateMetricSpecString(field=predictedFieldName,\n inferenceElement=InferenceElement.prediction,\n metric=\"trivial\",\n params={'window':metricWindow,\n \"errorMetric\":trivialErrorMetric,\n 'steps': steps})\n )\n\n ##Add in the One-Gram baseline error metric\n #metricSpecStrings.append(\n # _generateMetricSpecString(field=predictedFieldName,\n # inferenceElement=InferenceElement.encodings,\n # metric=\"two_gram\",\n # params={'window':metricWindow,\n # \"errorMetric\":oneGramErrorMetric,\n # 'predictionField':predictedFieldName,\n # 'steps': steps})\n # )\n #\n #Include the baseline moving mean/mode metric\n if isCategory:\n metricSpecStrings.append(\n _generateMetricSpecString(field=predictedFieldName,\n inferenceElement=InferenceElement.prediction,\n metric=movingAverageBaselineName,\n params={'window':metricWindow\n ,\"errorMetric\":\"avg_err\",\n \"mode_window\":200,\n \"steps\": steps})\n )\n else :\n metricSpecStrings.append(\n _generateMetricSpecString(field=predictedFieldName,\n inferenceElement=InferenceElement.prediction,\n metric=movingAverageBaselineName,\n params={'window':metricWindow\n ,\"errorMetric\":\"altMAPE\",\n \"mean_window\":200,\n \"steps\": steps})\n )\n\n\n\n\n # -----------------------------------------------------------------------\n # Metrics for classification\n elif inferenceType in (InferenceType.TemporalClassification):\n\n metricName = 'avg_err'\n trivialErrorMetric = 'avg_err'\n oneGramErrorMetric = 'avg_err'\n movingAverageBaselineName = 'moving_mode'\n\n optimizeMetricSpec, optimizeMetricLabel = \\\n _generateMetricSpecString(inferenceElement=InferenceElement.classification,\n metric=metricName,\n params={'window':metricWindow},\n returnLabel=True)\n\n metricSpecStrings.append(optimizeMetricSpec)\n\n if options[\"runBaselines\"]:\n # If temporal, generate the trivial predictor metric\n if inferenceType == InferenceType.TemporalClassification:\n metricSpecStrings.append(\n _generateMetricSpecString(inferenceElement=InferenceElement.classification,\n metric=\"trivial\",\n params={'window':metricWindow,\n \"errorMetric\":trivialErrorMetric})\n )\n metricSpecStrings.append(\n _generateMetricSpecString(inferenceElement=InferenceElement.classification,\n metric=\"two_gram\",\n params={'window':metricWindow,\n \"errorMetric\":oneGramErrorMetric})\n )\n metricSpecStrings.append(\n _generateMetricSpecString(inferenceElement=InferenceElement.classification,\n metric=movingAverageBaselineName,\n params={'window':metricWindow\n ,\"errorMetric\":\"avg_err\",\n \"mode_window\":200})\n )\n\n\n # Custom Error Metric\n if not options[\"customErrorMetric\"] == None :\n #If errorWindow is not specified, make it equal to the default window\n if not \"errorWindow\" in options[\"customErrorMetric\"]:\n options[\"customErrorMetric\"][\"errorWindow\"] = metricWindow\n optimizeMetricSpec = _generateMetricSpecString(\n inferenceElement=InferenceElement.classification,\n metric=\"custom\",\n params=options[\"customErrorMetric\"])\n optimizeMetricLabel = \".*custom_error_metric.*\"\n\n metricSpecStrings.append(optimizeMetricSpec)\n\n\n # -----------------------------------------------------------------------\n # If plug in the predictionSteps variable for any dynamically generated\n # prediction steps\n if options['dynamicPredictionSteps']:\n for i in range(len(metricSpecStrings)):\n metricSpecStrings[i] = metricSpecStrings[i].replace(\n \"'$REPLACE_ME'\", \"predictionSteps\")\n optimizeMetricLabel = optimizeMetricLabel.replace(\n \"'$REPLACE_ME'\", \".*\")\n return metricSpecStrings, optimizeMetricLabel", "def create_perf_attrib_stats(perf_attrib, risk_exposures):\n \"\"\"\n Takes perf attribution data over a period of time and computes annualized\n multifactor alpha, multifactor sharpe, risk exposures.\n \"\"\"\n summary = OrderedDict()\n total_returns = perf_attrib['total_returns']\n specific_returns = perf_attrib['specific_returns']\n common_returns = perf_attrib['common_returns']\n\n summary['Annualized Specific Return'] =\\\n ep.annual_return(specific_returns)\n summary['Annualized Common Return'] =\\\n ep.annual_return(common_returns)\n summary['Annualized Total Return'] =\\\n ep.annual_return(total_returns)\n\n summary['Specific Sharpe Ratio'] =\\\n ep.sharpe_ratio(specific_returns)\n\n summary['Cumulative Specific Return'] =\\\n ep.cum_returns_final(specific_returns)\n summary['Cumulative Common Return'] =\\\n ep.cum_returns_final(common_returns)\n summary['Total Returns'] =\\\n ep.cum_returns_final(total_returns)\n\n summary = pd.Series(summary, name='')\n\n annualized_returns_by_factor = [ep.annual_return(perf_attrib[c])\n for c in risk_exposures.columns]\n cumulative_returns_by_factor = [ep.cum_returns_final(perf_attrib[c])\n for c in risk_exposures.columns]\n\n risk_exposure_summary = pd.DataFrame(\n data=OrderedDict([\n (\n 'Average Risk Factor Exposure',\n risk_exposures.mean(axis='rows')\n ),\n ('Annualized Return', annualized_returns_by_factor),\n ('Cumulative Return', cumulative_returns_by_factor),\n ]),\n index=risk_exposures.columns,\n )\n\n return summary, risk_exposure_summary" ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Notebooks
- Google Colab
- Kaggle
Welcome to the community
The community tab is the place to discuss and collaborate with the HF community!