<\/path> \n<\/svg> <\/p>\nFlair\u662f\u4e00\u4e2a\u57fa\u4e8ePyTorch\u6784\u5efa\u7684NLP\u5f00\u53d1\u5305\uff0c\u5b83\u5728\u89e3\u51b3\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\uff08NER\uff09\u3001\u8bed\u53e5\u6807\u6ce8\uff08POS\uff09\u3001\u6587\u672c\u5206\u7c7b\u7b49NLP\u95ee\u9898\u65f6\u8fbe\u5230\u4e86\u5f53\u524d\u7684\u9876\u5c16\u6c34\u51c6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Flair\u6784\u5efa\u5b9a\u5236\u7684\u6587\u672c\u5206\u7c7b\u5668\u3002<\/p>\n
\u7b80\u4ecb<\/h3>\n
\u6587\u672c\u5206\u7c7b\u662f\u4e00\u79cd\u7528\u6765\u5c06\u8bed\u53e5\u6216\u6587\u6863\u5f52\u5165\u4e00\u4e2a\u6216\u591a\u4e2a\u5206\u7c7b\u7684\u6709\u76d1\u7763\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5783\u573e\u90ae\u4ef6\u8fc7\u6ee4\u3001\u60c5\u611f\u5206\u6790\u3001\u65b0\u6587\u7ae0\u5f52\u7c7b\u7b49\u4f17\u591a\u4e1a\u52a1\u9886\u57df\u3002<\/p>\n
\u5f53\u524d\u7edd\u5927\u591a\u6570\u9886\u5148\u7684\u6587\u672c\u5206\u7c7b\u65b9\u6cd5\u90fd\u4f9d\u8d56\u4e8e\u6587\u672c\u5d4c\u5165\u6280\u672f\uff0c\u5b83\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u9ad8\u7ef4\u7a7a\u95f4\u7684\u6570\u503c\u8868\u793a\uff0c\u53ef\u4ee5\u5c06\u6587\u6863\u3001\u53e5\u5b50\u3001\u5355\u6b21\u6216\u5b57\u7b26\u8868\u793a\u4e3a\u8fd9\u4e2a\u9ad8\u7ef4\u7a7a\u95f4\u7684\u4e00\u4e2a\u5411\u91cf\u3002<\/p>\n
Flair\u57fa\u4e8eZalando Research\u7684\u8bba\u6587\u201c\u7528\u4e8e\u4e32\u884c\u6807\u51c6\u7684\u4e0a\u4e0b\u6587\u76f8\u5173\u5b57\u7b26\u4e32\u5d4c\u5165\u201d\uff0c\u8bba\u6587\u7b97\u6cd5\u8868\u73b0\u53ef\u4ee5\u6bd9\u6389\u4e4b\u524d\u7684\u6700\u597d\u65b9\u6848\uff0c\u8be5\u7b97\u6cd5\u5728Flair\u4e2d\u5f97\u5230\u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u4ee5\u7528\u6765\u6784\u5efa\u6587\u672c\u5206\u7c7b\u5668\u3002<\/p>\n
1. \u51c6\u5907<\/h3>\n
Flair\u5b89\u88c5\u9700\u8981Python 3.6\uff0c\u6267\u884cpip\u5b89\u88c5\u5373\u53ef\uff1a<\/p>\n
~$ pip install flair\n<\/code><\/pre>\n\u4e0a\u9762\u7684\u547d\u4ee4\u5c06\u5b89\u88c5\u8fd0\u884cFlair\u6240\u9700\u8981\u7684\u4f9d\u8d56\u5305\uff0c\u5f53\u7136\u4e5f\u5305\u62ec\u4e86PyTorch\u3002<\/p>\n
2. \u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u9884\u7f6e\u5206\u7c7b\u6a21\u578b<\/h3>\n
\u6700\u65b0\u7684Flair 0.4\u7248\u672c\u5305\u542b\u6709\u4e24\u4e2a\u9884\u5148\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u3002\u4e00\u4e2a\u57fa\u4e8eIMDB\u6570\u636e\u96c6\u8bad\u7ec3\u7684\u60c5\u611f\u5206\u6790\u6a21\u578b\u548c\u4e00\u4e2a\u653b\u51fb\u6027\u8bed\u8a00\u63a2\u6d4b\u6a21\u578b\uff08\u5f53\u524d\u4ec5\u652f\u6301\u5fb7\u8bed\uff09\u3002<\/p>\n
\u53ea\u9700\u4e00\u4e2a\u547d\u4ee4\u5c31\u53ef\u4ee5\u4e0b\u8f7d\u3001\u5b58\u50a8\u5e76\u4f7f\u7528\u6a21\u578b\uff0c\u8fd9\u4f7f\u5f97\u9884\u7f6e\u6a21\u578b\u7684\u4f7f\u7528\u8fc7\u7a0b\u5f02\u5e38\u7b80\u5355\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u4f7f\u7528\u60c5\u611f\u5206\u6790\u6a21\u578b\uff1a<\/p>\n
from flair.models import TextClassifier\nfrom flair.data import Sentence\n\nclassifier = TextClassifier.load('en-sentiment')\n\nsentence = Sentence('Flair is pretty neat!')\nclassifier.predict(sentence)\n\n# print sentence with predicted labels\nprint('Sentence above is: ', sentence.labels)\n<\/code><\/pre>\n\u5f53\u7b2c\u4e00\u6b21\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0cFlari\u5c06\u4e0b\u8f7d\u60c5\u611f\u5206\u6790\u6a21\u578b\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u4fdd\u5b58\u5230\u672c\u5730\u7528\u6237\u4e3b\u76ee\u5f55\u7684.flair\u5b50\u76ee\u5f55\uff0c\u4e0b\u8f7d\u53ef\u80fd\u9700\u8981\u51e0\u5206\u949f\u3002<\/p>\n
\u4e0a\u9762\u7684\u4ee3\u7801\u9996\u5148\u8f7d\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u7136\u540e\u8f7d\u5165\u60c5\u611f\u5206\u6790\u6a21\u578b\u5230\u5185\u5b58\u4e2d\uff08\u5fc5\u8981\u65f6\u5148\u4e0b\u8f7d\uff09\uff0c\u63a5\u4e0b\u6765
\u5c31\u53ef\u4ee5\u9884\u6d4b\u201cFlair is pretty neat!\u201d\u7684\u60c5\u611f\u5206\u503c\u4e86\uff080~1\u4e4b\u95f4\uff09\u3002\u6700\u540e\u7684\u547d\u4ee4\u8f93\u5165\u7ed3\u679c\u4e3a\uff1a<\/p>\n
The sentence above is: [Positive (1.0)].\n<\/code><\/pre>\n\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\uff01\u73b0\u5728\u4f60\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u4ee3\u7801\u6574\u5408\u4e3a\u4e00\u4e2aREST API\uff0c\u63d0\u4f9b\u7c7b\u4f3c\u4e8egoogle\u4e91\u7aef\u60c5\u611f\u5206\u6790API\u7684\u529f\u80fd\u4e86\uff01<\/p>\n
3. \u8bad\u7ec3\u81ea\u5b9a\u4e49\u6587\u672c\u5206\u7c7b\u5668<\/h3>\n
\u8981\u8bad\u7ec3\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6587\u672c\u5206\u7c7b\u5668\uff0c\u9996\u5148\u9700\u8981\u4e00\u4e2a\u6807\u6ce8\u6587\u672c\u96c6\u3002Flair\u7684\u5206\u7c7b\u6570\u636e\u96c6\u683c\u5f0f\u57fa\u4e8eFacebook\u7684FastText\u683c\u5f0f\uff0c\u8981\u6c42\u5728\u6bcf\u4e00\u884c\u7684\u5f00\u59cb\u4f7f\u7528**label<\/strong>**\u524d\u7f00\u5b9a\u4e49\u4e00\u4e2a\u6216\u591a\u4e2a\u6807\u7b7e\u3002\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n__label__<class_1> <text>\n__label__<class_2> <text>\n<\/code><\/pre>\n\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u6211\u4eec\u5c06\u4f7f\u7528Kaggle\u7684SMS\u5783\u573e\u4fe1\u606f\u68c0\u6d4b\u6570\u636e\u96c6\u6765\u7528Flair\u6784\u5efa\u4e00\u4e2a\u5783\u573e\/\u975e\u5783\u573e\u5206\u7c7b\u5668\u3002\u8fd9\u4e2a\u6570\u636e\u96c6\u5f88\u9002\u5408\u6211\u4eec\u7684\u5b66\u4e60\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u5f88\u5c0f\uff0c\u53ea\u67095572\u884c\u6570\u636e\uff0c\u53ef\u4ee5\u5728\u5355\u4e2aCPU\u4e0a\u53ea\u82b1\u51e0\u5206\u949f\u5c31\u5b8c\u6210\u6a21\u578b\u7684\u8bad\u7ec3\u3002<\/p>\n
<\/p>\n
3.1 \u9884\u5904\u7406 - \u6784\u5efa\u6570\u636e\u96c6<\/h4>\n
\u9996\u5148\u4e0b\u8f7dKaggle\u4e0a\u7684\u6570\u636e\u96c6\uff0c\u5f97\u5230spam.csv\uff1b\u7136\u540e\u518d\u6570\u636e\u96c6\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884c\u6211\u4eec\u7684\u5904\u7406\u811a\u672c\uff0c\u5f97\u5230\u8bad\u7ec3\u96c6\u3001\u5f00\u53d1\u96c6\u548c\u6d4b\u8bd5\u96c6\uff1a<\/p>\n
import pandas as pd\ndata = pd.read_csv(\".\/spam.csv\", encoding='latin-1').sample(frac=1).drop_duplicates()\n\ndata = data[['v1', 'v2']].rename(columns={\"v1\":\"label\", \"v2\":\"text\"})\n \ndata['label'] = '__label__' + data['label'].astype(str)\n\ndata.iloc[0:int(len(data)*0.8)].to_csv('train.csv', sep='\\t', index = False, header = False)\ndata.iloc[int(len(data)*0.8):int(len(data)*0.9)].to_csv('test.csv', sep='\\t', index = False, header = False)\ndata.iloc[int(len(data)*0.9):].to_csv('dev.csv', sep='\\t', index = False, header = False);\n<\/code><\/pre>\n\u4e0a\u9762\u7684\u811a\u672c\u4f1a\u8fdb\u884c\u5254\u91cd\u548c\u968f\u673a\u4e71\u5e8f\u5904\u7406\uff0c\u5e76\u6309\u716780\/10\/10\u7684\u6bd4\u4f8b\u8fdb\u884c\u6570\u636e\u96c6\u7684\u5206\u5272\u3002\u811a\u672c\u6210\u529f\u6267\u884c\u540e\uff0c\u5c31\u4f1a\u5f97\u5230FastText\u683c\u5f0f\u7684\u4e09\u4e2a\u6570\u636e\u6587\u4ef6\uff1atrain.csv\u3001dev.csv\u548ctest.csv\u3002<\/p>\n
3.2 \u8bad\u7ec3\u81ea\u5b9a\u4e49\u6587\u672c\u5206\u7c7b\u6a21\u578b<\/h4>\n
\u7528\u4e0b\u9762\u7684\u811a\u672c\u8bad\u7ec3\u6a21\u578b\uff1a<\/p>\n
from flair.data_fetcher import NLPTaskDataFetcher\nfrom flair.embeddings import WordEmbeddings, FlairEmbeddings, DocumentLSTMEmbeddings\nfrom flair.models import TextClassifier\nfrom flair.trainers import ModelTrainer\nfrom pathlib import Path\n\ncorpus = NLPTaskDataFetcher.load_classification_corpus(Path('.\/'), test_file='train.csv', dev_file='dev.csv', train_file='test.csv')\n\nword_embeddings = [WordEmbeddings('glove'), FlairEmbeddings('news-forward-fast'), FlairEmbeddings('news-backward-fast')]\n\ndocument_embeddings = DocumentLSTMEmbeddings(word_embeddings, hidden_size=512, reproject_words=True, reproject_words_dimension=256)\n\nclassifier = TextClassifier(document_embeddings, label_dictionary=corpus.make_label_dictionary(), multi_label=False)\n\ntrainer = ModelTrainer(classifier, corpus)\n\ntrainer.train('.\/', max_epochs=20)\n<\/code><\/pre>\n\u7b2c\u4e00\u6b21\u8fd0\u884c\u4e0a\u9762\u8fd9\u4e2a\u811a\u672c\u65f6\uff0cFlair\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u6240\u9700\u8981\u7684\u5d4c\u5165\u6a21\u578b\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u51e0\u5206\u949f\uff0c\u7136\u540e\u63a5\u4e0b\u6765\u7684\u6574\u4e2a\u8bad\u7ec3\u8fc7\u7a0b\u8fd8\u9700\u8981\u5927\u7ea65\u5206\u949f\u3002<\/p>\n
\u811a\u672c\u9996\u5148\u8f7d\u5165\u9700\u8981\u7684\u5e93\u548c\u6570\u636e\u96c6\uff0c\u5f97\u5230\u4e00\u4e2acorpus\u5bf9\u8c61\u3002<\/p>\n
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5d4c\u5165\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2aFlair\u4e0a\u4e0b\u6587\u5b57\u7b26\u4e32\u5d4c\u5165\u548c\u4e00\u4e2aGloVe\u5355\u8bcd\u5d4c\u5165\uff0c\u8fd9\u4e2a\u5217\u8868\u63a5\u4e0b\u6765\u5c06\u4f5c\u4e3a\u6211\u4eec\u6587\u6863\u5d4c\u5165\u5bf9\u8c61\u7684\u8f93\u5165\u3002\u5806\u53e0\u548c\u6587\u672c\u5d4c\u5165\u662fFlair\u4e2d\u6700\u6709\u8da3\u7684\u611f\u5ff5\u4e4b\u4e00\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u5c06\u4e0d\u540c\u7684\u5d4c\u5165\u6574\u5408\u5728\u4e00\u8d77\u7684\u624b\u6bb5\uff0c\u4f60\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u4f20\u7edf\u7684\u5355\u8bcd\u5d4c\u5165\uff08\u4f8b\u5982GloVe\u3001word2vector\u3001ELMo\uff09\u548cFlair\u7684\u4e0a\u4e0b\u6587\u5b57\u7b26\u4e32\u5d4c\u5165\u3002\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u57fa\u4e8eLSTM\u7684\u65b9\u6cd5\u6765\u751f\u6210\u6587\u6863\u5d4c\u5165\uff0c\u5173\u4e8e\u8be5\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\u53ef\u4ee5\u53c2\u8003\u8fd9\u91cc\u3002<\/p>\n
\u6700\u540e\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u8bad\u7ec3\u6a21\u578b\u5e76\u751f\u6210\u4e24\u4e2a\u6a21\u578b\u6587\u4ef6\uff1afinal-model.pt\u548cbest-model.pt\u3002<\/p>\n
3.3 \u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u8fdb\u884c\u9884\u6d4b<\/h4>\n
\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5bfc\u51fa\u7684\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\u4e86\u3002\u811a\u672c\u5982\u4e0b\uff1a<\/p>\n
from flair.models import TextClassifier\nfrom flair.data import Sentence\n\nclassifier = TextClassifier.load_from_file('.\/best-model.pt')\n\nsentence = Sentence('Hi. Yes mum, I will...')\n\nclassifier.predict(sentence)\n\nprint(sentence.labels)\n<\/code><\/pre>\n\u4e0a\u9762\u7684\u4ee3\u7801\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n
[ham (1.0)]\n<\/code><\/pre>\n\u8fd9\u610f\u5473\u7740\u6a21\u578b100%\u7684\u786e\u4fe1\u6211\u4eec\u8f93\u5165\u7684\u793a\u4f8b\u6d88\u606f\u4e0d\u662f\u5783\u573e\u4fe1\u606f\u3002<\/p>\n
Flair\u662f\u5982\u4f55\u8d85\u8d8a\u5176\u4ed6\u6846\u67b6\u7684\uff1f<\/h3>\n
\u4e0eFacebook\u7684FastText\u6216\u8005Google\u7684AutoML\u5e73\u53f0\u4e0d\u540c\uff0c\u7528Flair\u8fdb\u884c\u6587\u672c\u5206\u7c7b\u8fd8\u662f\u76f8\u5bf9\u5e95\u5c42\u7684\u4efb\u52a1\u3002\u6211\u4eec\u53ef\u4ee5\u5b8c\u5168\u63a7\u5236\u6587\u672c\u5982\u4f55\u5d4c\u5165\uff0c\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u8bad\u7ec3\u7684\u53c2\u6570\u4f8b\u5982\u5b66\u4e60\u901f\u7387\u3001\u6279\u5927\u5c0f\u3001\u635f\u5931\u51fd\u6570\u3001\u4f18\u5316\u5668\u9009\u62e9\u7b56\u7565\u7b49\uff0c\u8fd9\u4e9b\u8d85\u53c2\u6570\u662f\u8981\u5b9e\u73b0\u6700\u4f18\u6027\u80fd\u6240\u5fc5\u987b\u8fdb\u884c\u8c03\u6574\u7684\u3002Flair\u63d0\u4f9b\u4e86\u8457\u540d\u7684\u8d85\u53c2\u6570\u8c03\u6574\u5e93Hyperopt\u7684\u4e00\u4e2a\u5c01\u88c5\u3002<\/p>\n
\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u51fa\u4e8e\u7b80\u5316\u8003\u8651\u6211\u4eec\u4f7f\u7528\u4e86\u9ed8\u8ba4\u7684\u8d85\u53c2\u6570\uff0c\u5f97\u5230\u7684Flair\u6a21\u578b\u7684f1-score\u572820\u4e2aepoch\u4e4b\u540e\u8fbe\u5230\u4e860.973\u3002<\/p>\n
\u4e3a\u4e86\u5bf9\u6bd4\uff0c\u6211\u4eec\u4f7f\u7528FastText\u548cAutoML\u8bad\u7ec3\u4e86\u4e00\u4e2a\u6587\u672c\u5206\u7c7b\u5668\u3002\u6211\u4eec\u9996\u5148\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\u8fd0\u884c
FastText\uff0c\u5f97\u5230\u7684f1-score\u4e3a0.883\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u7684Flair\u6a21\u578b\u8fdc\u8fdc\u4f18\u4e8eFastText\u6a21\u578b\uff0c\u4e0d\u8fc7FastText\u7684\u8bad\u7ec3\u5f88\u5feb\uff0c\u53ea\u9700\u8981\u51e0\u79d2\u949f\u3002<\/p>\n
\u7136\u540e\u6211\u4eec\u4e5f\u4e0eAutoML\u81ea\u7136\u8bed\u8a00\u5e73\u53f0\u4e0a\u5f97\u5230\u7684\u7ed3\u679c\u8fdb\u884c\u4e86\u5bf9\u6bd4\u3002\u5e73\u53f0\u9996\u5148\u9700\u898120\u5206\u949f\u6765
\u89e3\u6790\u6570\u636e\u96c6\uff0c\u7136\u540e\u6211\u4eec\u542f\u52a8\u8bad\u7ec3\u8fc7\u7a0b\uff0c\u8fd9\u5927\u7ea6\u82b1\u4e863\u4e2a\u5c0f\u65f6\u624d\u5b8c\u6210\uff0c\u4f46\u662ff1-score\u8fbe\u5230\u4e86
99.211\uff0c\u8981\u7a0d\u597d\u4e8e\u6211\u4eec\u81ea\u5df1\u8bad\u7ec3\u7684Flair\u6a21\u578b\u3002<\/p>\n
\u539f\u6587\u94fe\u63a5\uff1a\u7528Flair\u8fdb\u884c\u6587\u672c\u5206\u7c7b - \u6c47\u667a\u7f51<\/p>\n","protected":false},"excerpt":{"rendered":"flair\u6587\u672c\u5206\u7c7bFlair\u662f\u4e00\u4e2a\u57fa\u4e8ePyTorch\u6784\u5efa\u7684NLP\u5f00\u53d1\u5305\uff0c\u5b83\u5728\u89e3\u51b3\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\uff08NER\uff09\u3001\u8bed\u53e5\u6807\u6ce8\uff08POS\uff09\u3001\u6587\u672c\u5206\u7c7b\u7b49NLP\u95ee\u9898\u65f6...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6598"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6598"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6598\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}