模型:
可持续投资领域的文本挖掘用BERT模型
可持续投资中的文本挖掘
ESG-BERT的应用可以远远超出文本分类。它可以进行细化调整,以执行可持续投资领域其他各种下游NLP任务。
该模型不应用于有意为人们创造敌对或疏远环境。
已经进行了大量研究,探讨了语言模型的偏见和公正性问题(见,例如, Sheng et al. (2021) 和 Bender et al. (2021) )。模型生成的预测可能包含针对受保护的类别、身份特征以及敏感的社会和职业群体的令人不安和有害的刻板印象。
用户(直接和下游用户)应了解模型的风险、偏见和限制。需要更多信息以获取进一步的建议。
需要更多信息
需要更多信息
需要更多信息
文本分类的经过微调的模型也可以使用 here 直接进行预测。首先,下载经过微调的pytorch_model.bin、config.json和vocab.txt。
需要更多信息
需要更多信息
ESG-BERT在无结构文本数据上进一步训练,针对下一句预测和掩码语言模型任务的准确率分别为100%和98%。针对文本分类的ESG-BERT的微调产生了0.90的F1分数。相比之下,经过微调的通用BERT(BERT-base)模型得分为0.79,而sci-kit学习方法得分为0.67。
需要更多信息
可使用 Machine Learning Impact calculator 中的方法估计碳排放。
需要更多信息
需要更多信息
需要更多信息
需要JDK 11来提供模型的服务
BibTeX:
需要更多信息
APA:
需要更多信息
需要更多信息
需要更多信息
与Ezi Ozoani和HuggingFace团队合作的 Mukut Mukherjee , Charan Pothireddi 和 Parabole.ai
需要更多信息
使用下面的代码开始使用模型。
点击展开pip install torchserve torch-model-archiver pip install torchvision pip install transformers
接下来,我们将设置处理器脚本。它是一个基本的文本分类处理器,可以进行改进。将此脚本另存为“handler.py”到您的目录中[1]
from abc import ABC
import json
import logging
import os
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from ts.torch_handler.base_handler import BaseHandler
logger = logging.getLogger(__name__)
class TransformersClassifierHandler(BaseHandler, ABC):
"""
Transformers text classifier handler class. This handler takes a text (string) and
as input and returns the classification text based on the serialized transformers checkpoint.
"""
def __init__(self):
super(TransformersClassifierHandler, self).__init__()
self.initialized = False
def initialize(self, ctx):
self.manifest = ctx.manifest
properties = ctx.system_properties
model_dir = properties.get("model_dir")
self.device = torch.device("cuda:" + str(properties.get("gpu_id")) if torch.cuda.is_available() else "cpu")
# Read model serialize/pt file
self.model = AutoModelForSequenceClassification.from_pretrained(model_dir)
self.tokenizer = AutoTokenizer.from_pretrained(model_dir)
self.model.to(self.device)
self.model.eval()
logger.debug('Transformer model from path {0} loaded successfully'.format(model_dir))
# Read the mapping file, index to object name
mapping_file_path = os.path.join(model_dir, "index_to_name.json")
if os.path.isfile(mapping_file_path):
with open(mapping_file_path) as f:
self.mapping = json.load(f)
else:
logger.warning('Missing the index_to_name.json file. Inference output will not include class name.')
self.initialized = True
def preprocess(self, data):
""" Very basic preprocessing code - only tokenizes.
Extend with your own preprocessing steps as needed.
"""
text = data[0].get("data")
if text is None:
text = data[0].get("body")
sentences = text.decode('utf-8')
logger.info("Received text: '%s'", sentences)
inputs = self.tokenizer.encode_plus(
sentences,
add_special_tokens=True,
return_tensors="pt"
)
return inputs
def inference(self, inputs):
"""
Predict the class of a text using a trained transformer model.
"""
# NOTE: This makes the assumption that your model expects text to be tokenized
# with "input_ids" and "token_type_ids" - which is true for some popular transformer models, e.g. bert.
# If your transformer model expects different tokenization, adapt this code to suit
# its expected input format.
prediction = self.model(
inputs['input_ids'].to(self.device),
token_type_ids=inputs['token_type_ids'].to(self.device)
)[0].argmax().item()
logger.info("Model predicted: '%s'", prediction)
if self.mapping:
prediction = self.mapping[str(prediction)]
return [prediction]
def postprocess(self, inference_output):
# TODO: Add any needed post-processing of the model predictions here
return inference_output
_service = TransformersClassifierHandler()
def handle(data, context):
try:
if not _service.initialized:
_service.initialize(context)
if data is None:
return None
data = _service.preprocess(data)
data = _service.inference(data)
data = _service.postprocess(data)
return data
except Exception as e:
raise e
TorchServe使用一种称为MAR(模型归档)的格式。我们可以使用此命令将PyTorch模型转换为.mar文件:
torch-model-archiver --model-name "bert" --version 1.0 --serialized-file ./bert_model/pytorch_model.bin --extra-files "./bert_model/config.json,./bert_model/vocab.txt" --handler "./handler.py"
将.mar文件移至一个新目录:
mkdir model_store && mv bert.mar model_store
最后,我们可以使用以下命令启动TorchServe:
torchserve --start --model-store model_store --models bert=bert.mar
现在,我们可以使用推理API从另一个终端窗口查询模型。我们传递一个包含模型将尝试分类的文本的文本文件。
curl -X POST http://127.0.0.1:8080/predictions/bert -T predict.txt
这将返回一个与文本标签对应的标签号码。这存储在label_dict.txt词典文件中。
__label__Business_Ethics : 0 __label__Data_Security : 1 __label__Access_And_Affordability : 2 __label__Business_Model_Resilience : 3 __label__Competitive_Behavior : 4 __label__Critical_Incident_Risk_Management : 5 __label__Customer_Welfare : 6 __label__Director_Removal : 7 __label__Employee_Engagement_Inclusion_And_Diversity : 8 __label__Employee_Health_And_Safety : 9 __label__Human_Rights_And_Community_Relations : 10 __label__Labor_Practices : 11 __label__Management_Of_Legal_And_Regulatory_Framework : 12 __label__Physical_Impacts_Of_Climate_Change : 13 __label__Product_Quality_And_Safety : 14 __label__Product_Design_And_Lifecycle_Management : 15 __label__Selling_Practices_And_Product_Labeling : 16 __label__Supply_Chain_Management : 17 __label__Systemic_Risk_Management : 18 __label__Waste_And_Hazardous_Materials_Management : 19 __label__Water_And_Wastewater_Management : 20 __label__Air_Quality : 21 __label__Customer_Privacy : 22 __label__Ecological_Impacts : 23 __label__Energy_Management : 24 __label__GHG_Emissions : 25
<\details>