{"id":10389,"date":"2026-11-11T18:45:57","date_gmt":"2026-11-11T09:45:57","guid":{"rendered":"https:\/\/since2020.jp\/media\/?p=10389"},"modified":"2026-11-11T18:45:57","modified_gmt":"2026-11-11T09:45:57","slug":"bert-tutorial","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/bert-tutorial\/","title":{"rendered":"BERT\u30c6\u30ad\u30b9\u30c8\u5206\u985e \u2015 \u82f1\u8a9e\u306e\u6620\u753b\u30ec\u30d3\u30e5\u30fc\u3092\u300c\u80af\u5b9a\u30fb\u5426\u5b9a\u300d\u306b\u5206\u3051\u3066\u307f\u308b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u306f\u3058\u3081\u306b<\/h2>\n\n\n\n<p>\u300c\u3053\u306e\u30e1\u30fc\u30eb\u306f\u8ff7\u60d1\u30e1\u30fc\u30eb\u304b\u300d\u300c\u3053\u306e\u30ec\u30d3\u30e5\u30fc\u306f\u8912\u3081\u3066\u3044\u308b\u304b\u3001\u3051\u306a\u3057\u3066\u3044\u308b\u304b\u300d\u300c\u3053\u306e\u30cb\u30e5\u30fc\u30b9\u306f\u30b9\u30dd\u30fc\u30c4\u304b\u3001\u7d4c\u6e08\u304b\u300d\u3002\u3053\u3046\u3057\u305f\u6587\u7ae0\u3092\u3044\u304f\u3064\u304b\u306e\u30b0\u30eb\u30fc\u30d7\u306b\u632f\u308a\u5206\u3051\u308b\u4f5c\u696d\u3092 <strong>\u30c6\u30ad\u30b9\u30c8\u5206\u985e<\/strong> \u3068\u547c\u3073\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u8eab\u306e\u56de\u308a\u306e\u591a\u304f\u306e\u30b5\u30fc\u30d3\u30b9\u3067\u4f7f\u308f\u308c\u3066\u3044\u308b\u6280\u8853\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u81ea\u7136\u8a00\u8a9e\u51e6\u7406\u306e\u5b9a\u756a\u30e2\u30c7\u30eb <strong>BERT<\/strong> \u3092\u4f7f\u3044\u3001\u82f1\u8a9e\u306e\u6620\u753b\u30ec\u30d3\u30e5\u30fc\u3092\u300c\u80af\u5b9a\uff08POSITIVE\uff09\u300d\u3068\u300c\u5426\u5b9a\uff08NEGATIVE\uff09\u300d\u306b\u5206\u3051\u308b\u3068\u3053\u308d\u307e\u3067\u3001\u5b9f\u969b\u306b\u624b\u3092\u52d5\u304b\u3057\u306a\u304c\u3089\u4f53\u9a13\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u306e\u30b4\u30fc\u30eb\u306f\u6b21\u306e3\u3064\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>BERT\u304c\u4f55\u3092\u3057\u3066\u3044\u308b\u306e\u304b\u3001\u3056\u3063\u304f\u308a\u8aac\u660e\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b<\/li>\n\n\n\n<li>\u516c\u958b\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u3001BERT\u3092\u5206\u985e\u7528\u306b\u5b66\u7fd2\u3055\u305b\u308b\uff08\u30d5\u30a1\u30a4\u30f3\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\uff09<\/li>\n\n\n\n<li>\u81ea\u5206\u3067\u66f8\u3044\u305f\u82f1\u6587\u3092\u5165\u529b\u3057\u3001\u80af\u5b9a\u304b\u5426\u5b9a\u304b\u3092\u4e88\u6e2c\u3055\u305b\u308b<\/li>\n<\/ul>\n\n\n\n<p><strong>GPU\u306e\u306a\u3044\u30d1\u30bd\u30b3\u30f3\u3067\u3082\u52d5\u304f<\/strong> \u3088\u3046\u3001\u8a08\u7b97\u91cf\u3092\u6291\u3048\u3066\u3044\u307e\u3059\u3002\u4f1a\u793e\u306e\u30d1\u30bd\u30b3\u30f3\u3067\u3082\u5b89\u5fc3\u3057\u3066\u8a66\u305b\u308b\u3088\u3046\u3001\u5b89\u5168\u306a\u9032\u3081\u65b9\u3082\u3042\u308f\u305b\u3066\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. BERT\u3068\u306f\uff1f<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<h3 class=\"wp-block-heading\">\u5f93\u6765\u306e\u624b\u6cd5\u3068\u306e\u9055\u3044<\/h3>\n<\/div><\/div>\n\n\n\n<p>BERT\u304c\u767b\u5834\u3059\u308b\u524d\u306f\u3001\u6587\u7ae0\u4e2d\u306e\u5358\u8a9e\u306e\u51fa\u73fe\u56de\u6570\u3092\u6570\u3048\u3066\u5206\u985e\u3059\u308b\u65b9\u6cd5\u304c\u3088\u304f\u4f7f\u308f\u308c\u3066\u3044\u307e\u3057\u305f\u3002\u300cgreat\u300d\u304c\u591a\u3051\u308c\u3070\u80af\u5b9a\u3001\u300cboring\u300d\u304c\u591a\u3051\u308c\u3070\u5426\u5b9a\u3001\u3068\u3044\u3046\u8003\u3048\u65b9\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u65b9\u6cd5\u306f\u5358\u7d14\u3067\u308f\u304b\u308a\u3084\u3059\u3044\u4e00\u65b9\u3067\u3001<strong>\u6587\u8108\u3092\u8aad\u307f\u53d6\u308c\u306a\u3044<\/strong>\u3068\u3044\u3046\u5f31\u70b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u300cThis movie is <strong>not<\/strong> good.\u300d\u306b\u306f\u300cgood\u300d\u304c\u542b\u307e\u308c\u307e\u3059\u304c\u3001\u610f\u5473\u306f\u5426\u5b9a\u3067\u3059<\/li>\n\n\n\n<li>\u300cbank\u300d\u306f\u300c\u9280\u884c\u300d\u3068\u3082\u300c\u5ddd\u306e\u571f\u624b\u300d\u3068\u3082\u8aad\u3081\u307e\u3059<\/li>\n<\/ul>\n\n\n\n<p><strong>BERT<\/strong>\uff08Bidirectional Encoder Representations from Transformers\uff09\u306f\u30012018\u5e74\u306bGoogle\u304c\u767a\u8868\u3057\u305f\u30e2\u30c7\u30eb\u3067\u3059\u3002\u6587\u306e <strong>\u524d\u5f8c\u306e\u4e21\u65b9\u5411<\/strong> \u3092\u540c\u6642\u306b\u898b\u306a\u304c\u3089\u3001\u5404\u5358\u8a9e\u306e\u610f\u5473\u3092\u6587\u8108\u306b\u5408\u308f\u305b\u3066\u3068\u3089\u3048\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u300cnot good\u300d\u306e\u3088\u3046\u306a\u8a00\u3044\u56de\u3057\u3082\u6b63\u3057\u304f\u6271\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u300c\u4e8b\u524d\u5b66\u7fd2\u300d\u3068\u300c\u30d5\u30a1\u30a4\u30f3\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u300d<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"481\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-11-1024x481.png\" alt=\"\" class=\"wp-image-10399\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-11-1024x481.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-11-300x141.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-11-768x361.png 768w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-11-1536x722.png 1536w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-11.png 1939w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e8b\u524d\u5b66\u7fd2<\/strong>\uff1aWikipedia\u306a\u3069\u306e\u5927\u91cf\u306e\u6587\u7ae0\u3092\u8aad\u307f\u3001\u300c\u82f1\u8a9e\u3068\u306f\u3053\u3046\u3044\u3046\u3082\u306e\u3060\u300d\u3068\u3044\u3046\u4e00\u822c\u7684\u306a\u77e5\u8b58\u3092\u8eab\u306b\u3064\u3051\u307e\u3059\u3002\u3053\u306e\u5de5\u7a0b\u306fGoogle\u306a\u3069\u304c\u3059\u3067\u306b\u6e08\u307e\u305b\u3066\u516c\u958b\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u79c1\u305f\u3061\u304c\u884c\u3046\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/li>\n\n\n\n<li><strong>\u30d5\u30a1\u30a4\u30f3\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0<\/strong>\uff1a\u4e8b\u524d\u5b66\u7fd2\u6e08\u307f\u306eBERT\u306b\u3001\u76ee\u7684\u306b\u5408\u3063\u305f\u5c11\u91cf\u306e\u30c7\u30fc\u30bf\uff08\u4eca\u56de\u306f\u6620\u753b\u30ec\u30d3\u30e5\u30fc\u3068\u305d\u306e\u6b63\u89e3\u30e9\u30d9\u30eb\uff09\u3067\u8ffd\u52a0\u306e\u5b66\u7fd2\u3092\u884c\u3044\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u305f\u3068\u3048\u308b\u306a\u3089\u3001\u5e45\u5e83\u304f\u57fa\u790e\u3092\u5b66\u3093\u3060\u4eba\u304c\u3001\u5c31\u8077\u5f8c\u306b\u5c02\u9580\u306e\u7df4\u7fd2\u554f\u984c\u3092\u5c11\u3057\u89e3\u304f\u3060\u3051\u3067\u4ed5\u4e8b\u3092\u3053\u306a\u305b\u308b\u3088\u3046\u306b\u306a\u308b\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002\u571f\u53f0\u3068\u306a\u308b\u77e5\u8b58\u304c\u3042\u308b\u305f\u3081\u3001\u5c11\u306a\u3044\u30c7\u30fc\u30bf\u3067\u3082\u9ad8\u3044\u7cbe\u5ea6\u304c\u5f97\u3089\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5206\u985e\u306b\u4f7f\u3046\u306e\u306f[CLS]<\/h3>\n\n\n\n<p>BERT\u306b\u6587\u7ae0\u3092\u5165\u308c\u308b\u3068\u304d\u3001\u5148\u982d\u306b <code>[CLS]<\/code> \u3068\u3044\u3046\u7279\u5225\u306a\u8a18\u53f7\u3092\u3064\u3051\u307e\u3059\u3002BERT\u306e\u4e2d\u3067\u6587\u5168\u4f53\u3092\u8aad\u307f\u7d42\u3048\u308b\u3068\u3001\u3053\u306e <code>[CLS]<\/code> \u306e\u4f4d\u7f6e\u306e\u51fa\u529b\u304c <strong>\u6587\u5168\u4f53\u306e\u8981\u7d04<\/strong> \u306e\u3088\u3046\u306a\u5f79\u5272\u3092\u6301\u3061\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u8981\u7d04\u306e\u4e0a\u306b\u5c0f\u3055\u306a\u300c\u5206\u985e\u7528\u306e\u5c64\u300d\u30921\u679a\u306e\u305b\u3066\u3001\u300c\u80af\u5b9a\u3067\u3042\u308b\u78ba\u7387\u300d\u3068\u300c\u5426\u5b9a\u3067\u3042\u308b\u78ba\u7387\u300d\u3092\u51fa\u3055\u305b\u307e\u3059\u3002\u3053\u308c\u304cBERT\u3067\u30c6\u30ad\u30b9\u30c8\u5206\u985e\u3092\u3059\u308b\u4ed5\u7d44\u307f\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4eca\u56de\u4f7f\u3046\u306e\u306f\u300cDistilBERT\u300d<\/h3>\n\n\n\n<p>\u672c\u5bb6\u306eBERT\u306f\u5927\u304d\u304f\u3001GPU\u304c\u306a\u3044\u30d1\u30bd\u30b3\u30f3\u3067\u306f\u5b66\u7fd2\u306b\u6642\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002\u305d\u3053\u3067\u4eca\u56de\u306f\u3001BERT\u3092\u5c0f\u3055\u304f\u4f5c\u308a\u76f4\u3057\u305f <strong>DistilBERT<\/strong> \u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5927\u304d\u3055\u306f\u7d0440%\u5c0f\u3055\u304f\u3001\u51e6\u7406\u306f\u7d0460%\u901f\u3044<\/li>\n\n\n\n<li>\u6027\u80fd\u306f\u5143\u306eBERT\u306e\u7d0497%\u3092\u4fdd\u3064<\/li>\n<\/ul>\n\n\n\n<p>\u4f7f\u3044\u65b9\u306f\u672c\u5bb6\u306eBERT\u3068\u307e\u3063\u305f\u304f\u540c\u3058\u3067\u3059\u3002\u8a18\u4e8b\u306e\u6700\u5f8c\u306b\u3001\u672c\u5bb6\u306eBERT\u306b\u66ff\u3048\u308b\u65b9\u6cd5\u3082\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u5206\u985e\u306e\u6d41\u308c\uff08\u5168\u4f53\u50cf\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"194\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-12-1024x194.png\" alt=\"\" class=\"wp-image-10400\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-12-1024x194.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-12-300x57.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-12-768x145.png 768w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-12-1536x291.png 1536w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/11\/image-12-2048x387.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u6e96\u5099<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u5168\u306b\u9032\u3081\u308b\u305f\u3081\u306e\u6c7a\u307e\u308a<\/h3>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u6b21\u306e\u6c7a\u307e\u308a\u3092\u5b88\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6c7a\u307e\u308a<\/th><th>\u7406\u7531<\/th><\/tr><\/thead><tbody><tr><td>\u4eee\u60f3\u74b0\u5883\uff08venv\uff09\u3092\u4f5c\u3063\u3066\u3001\u305d\u306e\u4e2d\u306b\u3060\u3051\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5165\u308c\u308b<\/td><td>\u30d1\u30bd\u30b3\u30f3\u5168\u4f53\u306e\u74b0\u5883\u3092\u6c5a\u3055\u305a\u3001\u7d42\u308f\u3063\u305f\u3089\u30d5\u30a9\u30eb\u30c0\u3054\u3068\u6d88\u305b\u308b<\/td><\/tr><tr><td>\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u516c\u5f0f\u306e\u914d\u5e03\u5143\uff08PyPI\uff09\u304b\u3089\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u56fa\u5b9a\u3057\u3066\u5165\u308c\u308b<\/td><td>\u4f3c\u305f\u540d\u524d\u306e\u507d\u7269\u3084\u3001\u601d\u308f\u306c\u66f4\u65b0\u3092\u907f\u3051\u308b<\/td><\/tr><tr><td>\u30e2\u30c7\u30eb\u3068\u30c7\u30fc\u30bf\u306f\u3001Hugging Face \u306e\u516c\u5f0f\u306e\u3082\u306e\u3060\u3051\u3092\u4f7f\u3046<\/td><td>\u51fa\u3069\u3053\u308d\u306e\u306f\u3063\u304d\u308a\u3057\u305f\u3082\u306e\u3060\u3051\u3092\u4f7f\u3046<\/td><\/tr><tr><td><code>trust_remote_code=True<\/code> \u306f\u4f7f\u308f\u306a\u3044<\/td><td>\u5916\u90e8\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3057\u307e\u3046\u8a2d\u5b9a\u306e\u305f\u3081<\/td><\/tr><tr><td>\u30ed\u30b0\u30a4\u30f3\u3084\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u306f\u4f7f\u308f\u306a\u3044<\/td><td>\u516c\u958b\u30e2\u30c7\u30eb\u3068\u516c\u958b\u30c7\u30fc\u30bf\u306a\u306e\u3067\u4e0d\u8981<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f5c\u696d\u30d5\u30a9\u30eb\u30c0\u3068\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u308b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p ~\/bert-blog &amp;&amp; cd ~\/bert-blog\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 -m venv .venv\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>source .venv\/bin\/activate\n<\/code><\/pre>\n\n\n\n<p>\u884c\u306e\u5148\u982d\u306b <code>(.venv)<\/code> \u3068\u51fa\u308c\u3070\u3001\u4eee\u60f3\u74b0\u5883\u306b\u5165\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5165\u308c\u308b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install --upgrade pip\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install \"torch==2.5.1\" \"transformers==4.46.3\" \"datasets==3.1.0\" \"accelerate==1.1.1\" \"scikit-learn==1.5.2\"\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u30e9\u30a4\u30d6\u30e9\u30ea<\/th><th>\u5f79\u5272<\/th><\/tr><\/thead><tbody><tr><td><code>torch<\/code><\/td><td>\u6df1\u5c64\u5b66\u7fd2\u306e\u8a08\u7b97\u3092\u3059\u308b\u571f\u53f0\uff08PyTorch\uff09<\/td><\/tr><tr><td><code>transformers<\/code><\/td><td>BERT\u306a\u3069\u306e\u30e2\u30c7\u30eb\u3092\u7c21\u5358\u306b\u4f7f\u3048\u308b\u3088\u3046\u306b\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea<\/td><\/tr><tr><td><code>datasets<\/code><\/td><td>\u516c\u958b\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u30e9\u30a4\u30d6\u30e9\u30ea<\/td><\/tr><tr><td><code>accelerate<\/code><\/td><td>\u5b66\u7fd2\u3092\u52d5\u304b\u3059 <code>Trainer<\/code> \u304c\u5185\u90e8\u3067\u4f7f\u3046<\/td><\/tr><tr><td><code>scikit-learn<\/code><\/td><td>\u6b63\u89e3\u7387\u306a\u3069\u306e\u8a55\u4fa1\u6307\u6a19\u3092\u8a08\u7b97\u3059\u308b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">GPU\uff08MPS\uff09\u304c\u4f7f\u3048\u308b\u304b\u78ba\u304b\u3081\u308b<\/h3>\n\n\n\n<p>Apple \u30b7\u30ea\u30b3\u30f3\uff08M1\u301cM4\u306a\u3069\uff09\u306e Mac \u3067\u306f\u3001PyTorch \u306e <strong>MPS<\/strong> \u3068\u3044\u3046\u4ed5\u7d44\u307f\u3067\u3001Mac \u306b\u5185\u8535\u3055\u308c\u305fGPU\u3092\u4f7f\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python -c \"import torch; print('MPS:', torch.backends.mps.is_available())\"\n<\/code><\/pre>\n\n\n\n<p><code>MPS: True<\/code> \u306a\u3089\u5185\u8535GPU\u3001<code>False<\/code> \u306a\u3089CPU\u3067\u8a08\u7b97\u3057\u307e\u3059\u3002<strong>\u3069\u3061\u3089\u3067\u3082\u3053\u306e\u8a18\u4e8b\u306e\u30b3\u30fc\u30c9\u306f\u305d\u306e\u307e\u307e\u52d5\u304d\u307e\u3059\u3002<\/strong> \u5b66\u7fd2\u3092\u52d5\u304b\u3059 <code>Trainer<\/code> \u304c\u3001\u4f7f\u3048\u308b\u307b\u3046\u3092\u81ea\u52d5\u3067\u9078\u3073\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u30c7\u30fc\u30bf\u3092\u898b\u3066\u307f\u308b<\/h2>\n\n\n\n<p>\u3053\u3053\u304b\u3089\u306e\u30b3\u30fc\u30c9\u306f\u3001<code>bert_imdb.py<\/code> \u3068\u3044\u30461\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u4e0a\u304b\u3089\u9806\u306b\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\uff08\u8a18\u4e8b\u306e\u6700\u5f8c\u306b\u5168\u6587\u3092\u8f09\u305b\u3066\u3044\u307e\u3059\uff09\u3002<\/p>\n\n\n\n<p>\u4f7f\u3046\u30c7\u30fc\u30bf\u306f <strong>IMDb<\/strong> \u306e\u6620\u753b\u30ec\u30d3\u30e5\u30fc\u3067\u3059\u3002\u82f1\u8a9e\u306e\u30ec\u30d3\u30e5\u30fc5\u4e07\u4ef6\u306b\u300c\u80af\u5b9a\uff081\uff09\u300d\u304b\u300c\u5426\u5b9a\uff080\uff09\u300d\u306e\u30e9\u30d9\u30eb\u304c\u3064\u3044\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\n\n# Hugging Face \u3078\u306e\u5229\u7528\u72b6\u6cc1\u306e\u9001\u4fe1\u3092\u6b62\u3081\u308b\uff08\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u8aad\u307f\u8fbc\u3080\u524d\u306b\u8a2d\u5b9a\u3059\u308b\uff09\nos.environ&#091;\"HF_HUB_DISABLE_TELEMETRY\"] = \"1\"\n\nfrom collections import Counter\n\nimport numpy as np\nimport torch\nfrom datasets import load_dataset\nfrom sklearn.metrics import accuracy_score, confusion_matrix, f1_score\nfrom transformers import (\n    AutoModelForSequenceClassification,\n    AutoTokenizer,\n    DataCollatorWithPadding,\n    Trainer,\n    TrainingArguments,\n    set_seed,\n)\n\nset_seed(42)  # \u6bce\u56de\u540c\u3058\u7d50\u679c\u306b\u306a\u308b\u3088\u3046\u306b\u4e71\u6570\u3092\u56fa\u5b9a\u3059\u308b\n\nMODEL_NAME = \"distilbert\/distilbert-base-uncased\"\nMAX_LENGTH = 128   # 1\u4ef6\u3042\u305f\u308a\u306e\u30c8\u30fc\u30af\u30f3\u6570\u306e\u4e0a\u9650\nN_TRAIN = 2000     # \u5b66\u7fd2\u306b\u4f7f\u3046\u4ef6\u6570\nN_TEST = 500       # \u8a55\u4fa1\u306b\u4f7f\u3046\u4ef6\u6570\n\n# \u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\nimdb = load_dataset(\"stanfordnlp\/imdb\")\nprint(imdb)\n<\/code><\/pre>\n\n\n\n<p>\u521d\u56de\u306f\u3001Hugging Face \u304b\u3089\u30c7\u30fc\u30bf\uff08\u5408\u308f\u305b\u3066\u7d0484MB\uff09\u304c\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u308c\u307e\u3059\u30022\u56de\u76ee\u304b\u3089\u306f\u624b\u5143\u306b\u4fdd\u5b58\u3055\u308c\u305f\u3082\u306e\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DatasetDict({\n    train: Dataset({\n        features: &#091;'text', 'label'],\n        num_rows: 25000\n    })\n    test: Dataset({\n        features: &#091;'text', 'label'],\n        num_rows: 25000\n    })\n    unsupervised: Dataset({\n        features: &#091;'text', 'label'],\n        num_rows: 50000\n    })\n})\n<\/code><\/pre>\n\n\n\n<p>\u5b66\u7fd2\u7528\uff08train\uff09\u3068\u8a55\u4fa1\u7528\uff08test\uff09\u304c\u3001\u305d\u308c\u305e\u308c25,000\u4ef6\u305a\u3064\u3042\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<code>unsupervised<\/code> \u306f\u30e9\u30d9\u30eb\u306e\u3064\u3044\u3066\u3044\u306a\u3044\u30ec\u30d3\u30e5\u30fc\u3067\u3001\u4eca\u56de\u306f\u4f7f\u3044\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ef6\u6570\u3092\u6e1b\u3089\u3059<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>25,000\u4ef6\u3059\u3079\u3066\u3092\u4f7f\u3046\u3068\u3001CPU\u3067\u8a08\u7b97\u3059\u308b\u30d1\u30bd\u30b3\u30f3\u3067\u306f\u4f55\u6642\u9593\u3082\u304b\u304b\u308a\u307e\u3059\u3002\u4eca\u56de\u306f <strong>\u5b66\u7fd2\u75282,000\u4ef6\u3001\u8a55\u4fa1\u7528500\u4ef6<\/strong> \u306b\u7d5e\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>IMDb \u306e\u30c7\u30fc\u30bf\u306f <strong>\u30e9\u30d9\u30eb\u9806\u306b\u4e26\u3093\u3067\u3044\u308b<\/strong> \u306e\u3067\u3001\u5148\u982d\u304b\u3089\u53d6\u308b\u3068\u5168\u90e8\u300c\u5426\u5b9a\u300d\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u5fc5\u305a\u30b7\u30e3\u30c3\u30d5\u30eb\u3057\u3066\u304b\u3089\u53d6\u308a\u51fa\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>train_ds = imdb&#091;\"train\"].shuffle(seed=42).select(range(N_TRAIN))\ntest_ds = imdb&#091;\"test\"].shuffle(seed=42).select(range(N_TEST))\n\nprint(\"\u5b66\u7fd2\u7528\u306e\u30e9\u30d9\u30eb\u306e\u6570:\", Counter(train_ds&#091;\"label\"]))\nprint(\"\u8a55\u4fa1\u7528\u306e\u30e9\u30d9\u30eb\u306e\u6570:\", Counter(test_ds&#091;\"label\"]))\nprint(\"\u30ec\u30d3\u30e5\u30fc\u306e\u4f8b:\", train_ds&#091;0]&#091;\"text\"]&#091;:300])\nprint(\"\u30e9\u30d9\u30eb:\", train_ds&#091;0]&#091;\"label\"])\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5b66\u7fd2\u7528\u306e\u30e9\u30d9\u30eb\u306e\u6570: Counter({1: 1000, 0: 1000})\n\u8a55\u4fa1\u7528\u306e\u30e9\u30d9\u30eb\u306e\u6570: Counter({0: 254, 1: 246})\n\u30ec\u30d3\u30e5\u30fc\u306e\u4f8b: There is no relation at all between Fortier and Profiler \u2026\uff08\u4ee5\u4e0b\u7565\uff09\n\u30e9\u30d9\u30eb: 1\n<\/code><\/pre>\n\n\n\n<p>\u5b66\u7fd2\u7528\u306f\u3061\u3087\u3046\u30691,000\u4ef6\u305a\u3064\u3001\u8a55\u4fa1\u7528\u306f254\u4ef6\u3068246\u4ef6\u3067\u3001\u80af\u5b9a\u3068\u5426\u5b9a\u304c\u307b\u307c\u534a\u5206\u305a\u3064\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u504f\u308a\u304c\u306a\u3044\u306e\u3067\u3001\u3053\u306e\u307e\u307e\u4f7f\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u30c8\u30fc\u30af\u30f3\u5316\uff08\u6587\u3092\u6570\u5b57\u306b\u5909\u3048\u308b\uff09<\/h2>\n\n\n\n<p>BERT\u306f\u6587\u5b57\u3092\u305d\u306e\u307e\u307e\u8aad\u3081\u307e\u305b\u3093\u3002\u6587\u3092 <strong>\u30c8\u30fc\u30af\u30f3<\/strong>\uff08\u5358\u8a9e\u3084\u3001\u5358\u8a9e\u306e\u5207\u308c\u7aef\uff09\u306b\u5206\u3051\u3066\u3001\u305d\u308c\u305e\u308c\u3092\u756a\u53f7\u306b\u5909\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u4f5c\u696d\u3092 <strong>\u30c8\u30fc\u30af\u30f3\u5316<\/strong> \u3068\u547c\u3073\u3001\u30e2\u30c7\u30eb\u3068\u30bb\u30c3\u30c8\u3067\u516c\u958b\u3055\u308c\u3066\u3044\u308b <strong>\u30c8\u30fc\u30af\u30ca\u30a4\u30b6\u30fc<\/strong> \u304c\u884c\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n\nsample = tokenizer(\"This movie is not good.\")\nprint(sample)\nprint(tokenizer.convert_ids_to_tokens(sample&#091;\"input_ids\"]))\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{'input_ids': &#091;101, 2023, 3185, 2003, 2025, 2204, 1012, 102], 'attention_mask': &#091;1, 1, 1, 1, 1, 1, 1, 1]}\n&#091;'&#091;CLS]', 'this', 'movie', 'is', 'not', 'good', '.', '&#091;SEP]']\n<\/code><\/pre>\n\n\n\n<p>\u51fa\u529b\u306e\u610f\u5473\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>input_ids<\/code>\uff1a\u30c8\u30fc\u30af\u30f3\u306e\u756a\u53f7\u306e\u4e26\u3073\u3002\u5148\u982d\u306b <code>[CLS]<\/code>\uff08\u756a\u53f7101\uff09\u3001\u6700\u5f8c\u306b <code>[SEP]<\/code>\uff08\u756a\u53f7102\uff09\u304c\u81ea\u52d5\u3067\u5165\u308a\u307e\u3059\u3002\u300cThis\u300d\u304c\u5c0f\u6587\u5b57\u306e\u300cthis\u300d\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u306f\u3001\u4eca\u56de\u306e\u30e2\u30c7\u30eb\u304c\u5927\u6587\u5b57\u3068\u5c0f\u6587\u5b57\u3092\u533a\u5225\u3057\u306a\u3044\uff08uncased\uff09\u305f\u3081\u3067\u3059<\/li>\n\n\n\n<li><code>attention_mask<\/code>\uff1a\u300c\u3053\u3053\u306f\u672c\u7269\u306e\u30c8\u30fc\u30af\u30f3\uff081\uff09\u300d\u300c\u3053\u3053\u306f\u9577\u3055\u3092\u305d\u308d\u3048\u308b\u305f\u3081\u306e\u8a70\u3081\u7269\uff080\uff09\u300d\u306e\u76ee\u5370<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9577\u3044\u6587\u3068\u77ed\u3044\u6587\u306e\u6271\u3044<\/h3>\n\n\n\n<p>\u30ec\u30d3\u30e5\u30fc\u306e\u9577\u3055\u306f\u3070\u3089\u3070\u3089\u3067\u3001\u4e2d\u306b\u306f\u6570\u5343\u8a9e\u306e\u9577\u3044\u3082\u306e\u3082\u3042\u308a\u307e\u3059\u3002\u305d\u3053\u3067\u6b21\u306e2\u3064\u306e\u51e6\u7406\u3092\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5207\u308a\u6368\u3066\uff08truncation\uff09<\/strong>\uff1a\u4e0a\u9650\uff08\u4eca\u56de\u306f128\u30c8\u30fc\u30af\u30f3\uff09\u3092\u8d85\u3048\u305f\u90e8\u5206\u3092\u5207\u308a\u6368\u3066\u307e\u3059\u3002BERT\u81ea\u4f53\u306e\u4e0a\u9650\u306f512\u30c8\u30fc\u30af\u30f3\u3067\u3059\u304c\u3001\u77ed\u304f\u3059\u308b\u307b\u3069\u8a08\u7b97\u304c\u901f\u304f\u306a\u308a\u307e\u3059<\/li>\n\n\n\n<li><strong>\u8a70\u3081\u7269\uff08padding\uff09<\/strong>\uff1a\u307e\u3068\u3081\u3066\u8a08\u7b97\u3059\u308b\u3068\u304d\u306b\u3001\u77ed\u3044\u6587\u306e\u5f8c\u308d\u3092\u8a70\u3081\u7269\u3067\u57cb\u3081\u3066\u9577\u3055\u3092\u305d\u308d\u3048\u307e\u3059\u3002\u3053\u308c\u306f\u5b66\u7fd2\u306e\u3068\u304d\u306b <code>DataCollatorWithPadding<\/code> \u304c\u81ea\u52d5\u3067\u3084\u3063\u3066\u304f\u308c\u307e\u3059<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>def tokenize(batch):\n    return tokenizer(batch&#091;\"text\"], truncation=True, max_length=MAX_LENGTH)\n\ntrain_tok = train_ds.map(tokenize, batched=True, remove_columns=&#091;\"text\"])\ntest_tok = test_ds.map(tokenize, batched=True, remove_columns=&#091;\"text\"])\ndata_collator = DataCollatorWithPadding(tokenizer=tokenizer)\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>128\u30c8\u30fc\u30af\u30f3\u3067\u5207\u308a\u6368\u3066\u308b\u3068\u3001\u30ec\u30d3\u30e5\u30fc\u306e\u5f8c\u534a\u306f\u8aad\u307e\u308c\u307e\u305b\u3093\u3002\u305d\u308c\u3067\u3082\u5192\u982d\u3060\u3051\u3067\u80af\u5b9a\u30fb\u5426\u5b9a\u304c\u308f\u304b\u308b\u30ec\u30d3\u30e5\u30fc\u304c\u591a\u3044\u306e\u3067\u3001\u5341\u5206\u306a\u7cbe\u5ea6\u304c\u51fa\u307e\u3059\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u30d5\u30a1\u30a4\u30f3\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">7-1. \u30e2\u30c7\u30eb\u3092\u8aad\u307f\u8fbc\u3080<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>id2label = {0: \"NEGATIVE\", 1: \"POSITIVE\"}\nlabel2id = {\"NEGATIVE\": 0, \"POSITIVE\": 1}\n\nmodel = AutoModelForSequenceClassification.from_pretrained(\n    MODEL_NAME,\n    num_labels=2,\n    id2label=id2label,\n    label2id=label2id,\n    use_safetensors=True,  # \u5b89\u5168\u306a\u4fdd\u5b58\u5f62\u5f0f\u306e\u30d5\u30a1\u30a4\u30eb\u3060\u3051\u3092\u8aad\u307f\u8fbc\u3080\n)\n<\/code><\/pre>\n\n\n\n<p>\u521d\u56de\u306f\u3001\u30e2\u30c7\u30eb\u672c\u4f53\uff08\u7d04268MB\uff09\u304c\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5b9f\u884c\u3059\u308b\u3068\u300c\u4e00\u90e8\u306e\u91cd\u307f\u304c\u521d\u671f\u5316\u3055\u308c\u3066\u3044\u306a\u3044\uff08Some weights &#8230; newly initialized\uff09\u300d\u3068\u3044\u3046\u8b66\u544a\u304c\u51fa\u307e\u3059\u3002\u3053\u308c\u306f <strong>2-3<\/strong> \u3067\u8aac\u660e\u3057\u305f\u300c\u5206\u985e\u7528\u306e\u5c64\u300d\u304c\u307e\u3060\u4f55\u3082\u5b66\u3093\u3067\u3044\u306a\u3044\u3001\u3068\u3044\u3046\u610f\u5473\u306a\u306e\u3067\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002\u3053\u308c\u304b\u3089\u5b66\u7fd2\u3055\u305b\u308b\u90e8\u5206\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7-2. \u8a55\u4fa1\u306e\u3057\u304b\u305f\u3092\u6c7a\u3081\u308b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def compute_metrics(eval_pred):\n    logits, labels = eval_pred\n    preds = np.argmax(logits, axis=-1)\n    return {\n        \"accuracy\": accuracy_score(labels, preds),\n        \"f1\": f1_score(labels, preds),\n    }\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7-3. \u5b66\u7fd2\u306e\u8a2d\u5b9a\u3092\u3057\u3066\u3001\u52d5\u304b\u3059<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>args = TrainingArguments(\n    output_dir=\"out\",\n    num_train_epochs=1,               # \u30c7\u30fc\u30bf\u5168\u4f53\u3092\u4f55\u5468\u5b66\u7fd2\u3059\u308b\u304b\n    per_device_train_batch_size=16,   # \u4e00\u5ea6\u306b\u307e\u3068\u3081\u3066\u8a08\u7b97\u3059\u308b\u4ef6\u6570\n    per_device_eval_batch_size=32,\n    learning_rate=5e-5,               # 1\u56de\u306e\u5b66\u7fd2\u3067\u3069\u308c\u3060\u3051\u91cd\u307f\u3092\u52d5\u304b\u3059\u304b\n    weight_decay=0.01,                # \u5b66\u7fd2\u3057\u3059\u304e\uff08\u904e\u5b66\u7fd2\uff09\u3092\u6291\u3048\u308b\n    eval_strategy=\"epoch\",            # 1\u5468\u3054\u3068\u306b\u8a55\u4fa1\u3059\u308b\n    save_strategy=\"no\",               # \u9014\u4e2d\u7d4c\u904e\u306e\u30e2\u30c7\u30eb\u306f\u4fdd\u5b58\u3057\u306a\u3044\n    logging_steps=20,                 # 20\u56de\u3054\u3068\u306b\u640d\u5931\u3092\u8868\u793a\u3059\u308b\n    report_to=\"none\",                 # \u5916\u90e8\u306e\u8a18\u9332\u30b5\u30fc\u30d3\u30b9\u306b\u9001\u3089\u306a\u3044\n    seed=42,\n)\n\ntrainer = Trainer(\n    model=model,\n    args=args,\n    train_dataset=train_tok,\n    eval_dataset=test_tok,\n    data_collator=data_collator,\n    compute_metrics=compute_metrics,\n)\n\ntrainer.train()\n<\/code><\/pre>\n\n\n\n<p>\u8a2d\u5b9a\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u30a8\u30dd\u30c3\u30af\u6570\uff08<code>num_train_epochs<\/code>\uff09<\/strong>\uff1a2,000\u4ef6\u30921\u5468\u3060\u3051\u5b66\u7fd2\u3055\u305b\u307e\u3059\u3002BERT\u306e\u571f\u53f0\u304c\u3057\u3063\u304b\u308a\u3057\u3066\u3044\u308b\u306e\u3067\u30011\u5468\u3067\u3082\u304b\u306a\u308a\u7cbe\u5ea6\u304c\u51fa\u307e\u3059<\/li>\n\n\n\n<li><strong>\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba\uff08<code>per_device_train_batch_size<\/code>\uff09<\/strong>\uff1a16\u4ef6\u305a\u3064\u307e\u3068\u3081\u3066\u8a08\u7b97\u3057\u307e\u3059\u3002\u30e1\u30e2\u30ea\u304c\u8db3\u308a\u306a\u3044\u3068\u304d\u306f8\u306b\u4e0b\u3052\u307e\u3059<\/li>\n\n\n\n<li><strong>\u5b66\u7fd2\u7387\uff08<code>learning_rate<\/code>\uff09<\/strong>\uff1a\u30d5\u30a1\u30a4\u30f3\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u3067\u306f <code>2e-5<\/code>\u301c<code>5e-5<\/code> \u304c\u3088\u304f\u4f7f\u308f\u308c\u307e\u3059<\/li>\n\n\n\n<li><strong><code>report_to=\"none\"<\/code><\/strong>\uff1a\u521d\u671f\u8a2d\u5b9a\u306e\u307e\u307e\u3060\u3068\u3001\u5165\u3063\u3066\u3044\u308b\u8a18\u9332\u30b5\u30fc\u30d3\u30b9\u306b\u5b66\u7fd2\u306e\u7d4c\u904e\u3092\u9001\u308d\u3046\u3068\u3059\u308b\u3053\u3068\u304c\u3042\u308b\u306e\u3067\u3001\u660e\u793a\u7684\u306b\u6b62\u3081\u307e\u3059<\/li>\n<\/ul>\n\n\n\n<p>\u5b66\u7fd2\u4e2d\u306f <code>loss<\/code>\uff08\u640d\u5931\u3002\u30e2\u30c7\u30eb\u306e\u4e88\u6e2c\u304c\u6b63\u89e3\u304b\u3089\u3069\u308c\u3060\u3051\u305a\u308c\u3066\u3044\u308b\u304b\u3002\u5c0f\u3055\u3044\u307b\u3069\u3088\u3044\uff09\u304c20\u56de\u3054\u3068\u306b\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'loss': 0.6839, 'grad_norm': 1.6404471397399902, 'learning_rate': 4.2e-05, 'epoch': 0.16}\n{'loss': 0.5438, 'grad_norm': 6.623847007751465, 'learning_rate': 3.4000000000000007e-05, 'epoch': 0.32}\n{'loss': 0.4542, 'grad_norm': 8.676435470581055, 'learning_rate': 2.6000000000000002e-05, 'epoch': 0.48}\n{'loss': 0.4555, 'grad_norm': 6.553205966949463, 'learning_rate': 1.8e-05, 'epoch': 0.64}\n{'loss': 0.4173, 'grad_norm': 2.6448168754577637, 'learning_rate': 1e-05, 'epoch': 0.8}\n{'loss': 0.3684, 'grad_norm': 8.67974853515625, 'learning_rate': 2.0000000000000003e-06, 'epoch': 0.96}\n{'train_runtime': 151.9394, 'train_samples_per_second': 13.163, 'train_steps_per_second': 0.823, 'train_loss': 0.4850738296508789, 'epoch': 1.0}\n<\/code><\/pre>\n\n\n\n<p>\u8aad\u307f\u53d6\u308c\u308b\u3053\u3068\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>loss \u304c 0.68 \u304b\u3089 0.37 \u307e\u3067\u4e0b\u304c\u3063\u3066\u3044\u307e\u3059<\/strong>\u3002\u6700\u521d\u306e0.68\u306f\u30012\u629e\u3092\u5f53\u3066\u305a\u3063\u307d\u3046\u3067\u7b54\u3048\u305f\u3068\u304d\u3068\u307b\u307c\u540c\u3058\u5024\u3067\u3059\u3002\u305d\u3053\u304b\u3089\u5b66\u7fd2\u304c\u9032\u307f\u3001\u3060\u3093\u3060\u3093\u6b63\u3057\u304f\u5f53\u3066\u3089\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u304f\u69d8\u5b50\u304c\u308f\u304b\u308a\u307e\u3059<\/li>\n\n\n\n<li><strong>\u5b66\u7fd2\u7387\uff08<code>learning_rate<\/code>\uff09\u304c\u5c11\u3057\u305a\u3064\u5c0f\u3055\u304f\u306a\u3063\u3066\u3044\u307e\u3059<\/strong>\u3002\u6700\u521d\u306f\u5927\u304d\u304f\u91cd\u307f\u3092\u52d5\u304b\u3057\u3001\u7d42\u308f\u308a\u306b\u8fd1\u3065\u304f\u307b\u3069\u7d30\u304b\u304f\u8abf\u6574\u3059\u308b\u3001\u3068\u3044\u3046 <code>Trainer<\/code> \u306e\u521d\u671f\u8a2d\u5b9a\u306e\u52d5\u304d\u3067\u3059<\/li>\n\n\n\n<li><strong>\u5b66\u7fd2\u306b\u304b\u304b\u3063\u305f\u6642\u9593\u306f\u7d042\u520632\u79d2<\/strong>\uff08<code>train_runtime<\/code> \u304c151.9\u79d2\uff09\u3067\u3057\u305f\u3002Apple \u30b7\u30ea\u30b3\u30f3\u306e Mac \u306e\u5185\u8535GPU\uff08MPS\uff09\u3067\u30012,000\u4ef6\u309216\u4ef6\u305a\u3064125\u56de\u306b\u5206\u3051\u3066\u5b66\u7fd2\u3057\u3066\u3044\u307e\u3059<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">8. \u8a55\u4fa1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">8-1. \u6b63\u89e3\u7387\u3068F1\u30b9\u30b3\u30a2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>metrics = trainer.evaluate()\nprint(metrics)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{'eval_loss': 0.3923969566822052, 'eval_accuracy': 0.796, 'eval_f1': 0.7829787234042553, 'eval_runtime': 5.3985, 'eval_samples_per_second': 92.619, 'eval_steps_per_second': 2.964, 'epoch': 1.0}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6b63\u89e3\u7387\uff08Accuracy\uff09<\/strong>\uff1a500\u4ef6\u306e\u3046\u3061\u3001\u4f55\u5272\u3092\u6b63\u3057\u304f\u5f53\u3066\u305f\u304b<\/li>\n\n\n\n<li><strong>F1\u30b9\u30b3\u30a2<\/strong>\uff1a\u300c\u80af\u5b9a\u3068\u4e88\u6e2c\u3057\u305f\u3082\u306e\u306e\u3046\u3061\u3001\u672c\u5f53\u306b\u80af\u5b9a\u3060\u3063\u305f\u5272\u5408\uff08\u9069\u5408\u7387\uff09\u300d\u3068\u300c\u672c\u5f53\u306e\u80af\u5b9a\u306e\u3046\u3061\u3001\u80af\u5b9a\u3068\u5f53\u3066\u3089\u308c\u305f\u5272\u5408\uff08\u518d\u73fe\u7387\uff09\u300d\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u898b\u308b\u6307\u6a19\u30021\u306b\u8fd1\u3044\u307b\u3069\u3088\u3044<\/li>\n<\/ul>\n\n\n\n<p>\u79c1\u306e\u74b0\u5883\uff08Apple \u30b7\u30ea\u30b3\u30f3\u306e Mac\uff09\u3067\u306f\u3001<strong>\u6b63\u89e3\u7387\u306f79.6%\u3001F1\u30b9\u30b3\u30a2\u306f0.783<\/strong> \u3067\u3057\u305f\u3002500\u4ef6\u306e\u3046\u3061398\u4ef6\u3092\u6b63\u3057\u304f\u5f53\u3066\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5f53\u3066\u305a\u3063\u307d\u3046\u306a\u308950%\u306a\u306e\u3067\u3001\u5b66\u7fd2\u30c7\u30fc\u30bf\u3092\u305f\u3063\u305f2,000\u4ef6\u3001\u6587\u306e\u9577\u3055\u3092128\u30c8\u30fc\u30af\u30f3\u306b\u7d5e\u3063\u3066\u3082\u3001\u3057\u3063\u304b\u308a\u5b66\u7fd2\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002\u7cbe\u5ea6\u3092\u4e0a\u3052\u308b\u65b9\u6cd5\u306f\u3001\u8a18\u4e8b\u306e\u6700\u5f8c\u3067\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8-2. \u6df7\u540c\u884c\u5217\u3067\u3001\u3069\u3053\u3092\u9593\u9055\u3048\u305f\u304b\u898b\u308b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pred = trainer.predict(test_tok)\npred_labels = np.argmax(pred.predictions, axis=-1)\ncm = confusion_matrix(pred.label_ids, pred_labels)\n\nprint(\"                \u4e88\u6e2c:NEGATIVE  \u4e88\u6e2c:POSITIVE\")\nprint(f\"\u6b63\u89e3:NEGATIVE    {cm&#091;0]&#091;0]:&gt;10}  {cm&#091;0]&#091;1]:&gt;12}\")\nprint(f\"\u6b63\u89e3:POSITIVE    {cm&#091;1]&#091;0]:&gt;10}  {cm&#091;1]&#091;1]:&gt;12}\")\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>                \u4e88\u6e2c:NEGATIVE  \u4e88\u6e2c:POSITIVE\n\u6b63\u89e3:NEGATIVE           214            40\n\u6b63\u89e3:POSITIVE            62           184\n<\/code><\/pre>\n\n\n\n<p>\u5de6\u4e0a\u3068\u53f3\u4e0b\u304c\u300c\u6b63\u3057\u304f\u5f53\u3066\u305f\u4ef6\u6570\u300d\u3001\u53f3\u4e0a\u3068\u5de6\u4e0b\u304c\u300c\u9593\u9055\u3048\u305f\u4ef6\u6570\u300d\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5426\u5b9a\u306e\u30ec\u30d3\u30e5\u30fc254\u4ef6\u306e\u3046\u3061\u3001214\u4ef6\uff0884%\uff09\u3092\u6b63\u3057\u304f\u5426\u5b9a\u3068\u5f53\u3066\u307e\u3057\u305f<\/li>\n\n\n\n<li>\u80af\u5b9a\u306e\u30ec\u30d3\u30e5\u30fc246\u4ef6\u306e\u3046\u3061\u3001\u6b63\u3057\u304f\u80af\u5b9a\u3068\u5f53\u3066\u305f\u306e\u306f184\u4ef6\uff0875%\uff09\u3067\u300162\u4ef6\u3092\u5426\u5b9a\u3068\u9593\u9055\u3048\u307e\u3057\u305f<\/li>\n<\/ul>\n\n\n\n<p>\u3053\u306e\u30e2\u30c7\u30eb\u306f\u3001<strong>\u80af\u5b9a\u306e\u30ec\u30d3\u30e5\u30fc\u3092\u5426\u5b9a\u3068\u53d6\u308a\u9055\u3048\u308b\u307b\u3046\u304c\u591a\u3044<\/strong> \u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002\u6620\u753b\u30ec\u30d3\u30e5\u30fc\u306b\u306f\u300c\u524d\u534a\u306f\u9000\u5c48\u3060\u3063\u305f\u304c\u3001\u5f8c\u534a\u306f\u3059\u3070\u3089\u3057\u3044\u300d\u306e\u3088\u3046\u306b\u3001\u60aa\u3044\u70b9\u306b\u3075\u308c\u3066\u304b\u3089\u8912\u3081\u308b\u66f8\u304d\u65b9\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u306f\u5148\u982d\u306e128\u30c8\u30fc\u30af\u30f3\u3057\u304b\u8aad\u3093\u3067\u3044\u306a\u3044\u306e\u3067\u3001\u8912\u3081\u3066\u3044\u308b\u5f8c\u534a\u304c\u5207\u308a\u6368\u3066\u3089\u308c\u3066\u5426\u5b9a\u3068\u5224\u5b9a\u3055\u308c\u305f\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">9. \u81ea\u5206\u306e\u6587\u7ae0\u3067\u8a66\u3059<\/h2>\n\n\n\n<p>\u5b66\u7fd2\u3057\u305f\u30e2\u30c7\u30eb\u306b\u3001\u81ea\u5206\u3067\u66f8\u3044\u305f\u82f1\u6587\u3092\u5165\u308c\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def predict(texts):\n    model.eval()\n    inputs = tokenizer(\n        texts, truncation=True, max_length=MAX_LENGTH,\n        padding=True, return_tensors=\"pt\",\n    ).to(model.device)\n    with torch.no_grad():\n        probs = model(**inputs).logits.softmax(dim=-1)\n    for text, p in zip(texts, probs):\n        label = id2label&#091;int(p.argmax())]\n        print(f\"{label:8s} ({float(p.max()):.1%})  {text}\")\n\npredict(&#091;\n    \"This movie was absolutely wonderful. I loved every minute of it.\",\n    \"The plot was boring and the acting was terrible.\",\n    \"This movie is not good.\",\n    \"I expected to hate it, but it turned out to be great.\",\n    \"Oh great, another two hours of my life I will never get back.\",\n])\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>POSITIVE (93.3%)  This movie was absolutely wonderful. I loved every minute of it.\nNEGATIVE (94.0%)  The plot was boring and the acting was terrible.\nNEGATIVE (92.0%)  This movie is not good.\nPOSITIVE (81.7%)  I expected to hate it, but it turned out to be great.\nPOSITIVE (63.6%)  Oh great, another two hours of my life I will never get back.\n<\/code><\/pre>\n\n\n\n<p>\u304b\u3063\u3053\u306e\u4e2d\u306e\u6570\u5b57\u306f\u3001\u30e2\u30c7\u30eb\u304c\u305d\u306e\u5224\u5b9a\u306b\u3069\u308c\u3060\u3051\u81ea\u4fe1\u3092\u6301\u3063\u3066\u3044\u308b\u304b\uff08\u78ba\u7387\uff09\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1\u3064\u76ee\u30682\u3064\u76ee\u306e\u308f\u304b\u308a\u3084\u3059\u3044\u6587\u306f\u3001\u3069\u3061\u3089\u308290%\u4ee5\u4e0a\u306e\u81ea\u4fe1\u3067\u6b63\u3057\u304f\u5f53\u3066\u307e\u3057\u305f<\/li>\n\n\n\n<li>3\u3064\u76ee\u306e\u300cnot good\u300d\u306f\u3001\u300cgood\u300d\u304c\u5165\u3063\u3066\u3044\u3066\u3082 <strong>\u5426\u5b9a\uff0892.0%\uff09<\/strong> \u3068\u6b63\u3057\u304f\u5224\u5b9a\u3067\u304d\u307e\u3057\u305f\u3002\u5358\u8a9e\u3092\u6570\u3048\u308b\u3060\u3051\u306e\u6614\u306e\u65b9\u6cd5\u3067\u306f\u96e3\u3057\u304b\u3063\u305f\u6587\u3067\u3059<\/li>\n\n\n\n<li>4\u3064\u76ee\u306e\u300c\u5acc\u3044\u3060\u3068\u601d\u3063\u3066\u3044\u305f\u3051\u308c\u3069\u3001\u3088\u304b\u3063\u305f\u300d\u3068\u3044\u3046 <strong>\u9006\u8ee2<\/strong> \u306e\u6587\u3082\u3001\u80af\u5b9a\u3068\u5f53\u3066\u307e\u3057\u305f\u3002\u305f\u3060\u3001\u81ea\u4fe1\u306f81.7%\u3068\u5c11\u3057\u4e0b\u304c\u3063\u3066\u3044\u307e\u3059<\/li>\n<\/ul>\n\n\n\n<p>\u3044\u3061\u3070\u3093\u9762\u767d\u3044\u306e\u306f5\u3064\u76ee\u3067\u3059\u3002\u300cOh great\uff08\u3044\u3084\u3042\u3001\u3059\u3070\u3089\u3057\u3044\uff09\u300d\u3068\u8a00\u3044\u306a\u304c\u3089\u3001\u300c\u4e8c\u5ea6\u3068\u623b\u3089\u306a\u30442\u6642\u9593\u300d\u3068\u5606\u3044\u3066\u3044\u308b <strong>\u76ae\u8089<\/strong> \u306e\u6587\u3067\u3059\u3002\u4eba\u304c\u8aad\u3081\u3070\u660e\u3089\u304b\u306b\u5426\u5b9a\u3067\u3059\u304c\u3001\u30e2\u30c7\u30eb\u306f <strong>\u80af\u5b9a\uff0863.6%\uff09<\/strong> \u3068\u9593\u9055\u3048\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u305f\u3060\u3057\u3001\u81ea\u4fe1\u306f63.6%\u3068\u4f4e\u304f\u3001\u30e2\u30c7\u30eb\u3082\u300c\u8ff7\u3063\u3066\u3044\u308b\u300d\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002\u8a00\u8449\u306e\u8868\u9762\u3068\u672c\u5f53\u306e\u610f\u5473\u304c\u9006\u306b\u306a\u308b\u76ae\u8089\u306f\u3001BERT\u3067\u3082\u82e6\u624b\u306a\u3053\u3068\u304c\u591a\u3044\u6587\u3067\u3059\u30022,000\u4ef6\u306e\u5b66\u7fd2\u30c7\u30fc\u30bf\u3067\u306f\u3001\u3053\u3053\u307e\u3067\u8aad\u307f\u53d6\u308b\u306e\u306f\u96e3\u3057\u304b\u3063\u305f\u3088\u3046\u3067\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">10. \u3064\u307e\u305a\u304d\u3084\u3059\u3044\u30dd\u30a4\u30f3\u30c8<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u56f0\u308a\u3054\u3068<\/th><th>\u5bfe\u51e6<\/th><\/tr><\/thead><tbody><tr><td>\u30e1\u30e2\u30ea\u4e0d\u8db3\u306e\u30a8\u30e9\u30fc\u304c\u51fa\u308b<\/td><td><code>per_device_train_batch_size<\/code> \u30928\u30844\u306b\u4e0b\u3052\u308b\u3002<code>MAX_LENGTH<\/code> \u309264\u306b\u4e0b\u3052\u308b<\/td><\/tr><tr><td>\u5b66\u7fd2\u304c\u3068\u3066\u3082\u9045\u3044<\/td><td><code>N_TRAIN<\/code> \u30921,000\u306b\u6e1b\u3089\u3059\u3002\u7b46\u8005\u306e\u74b0\u5883\uff08Apple \u30b7\u30ea\u30b3\u30f3\u3001MPS\uff09\u3067\u306f\u5b66\u7fd2\u306f\u7d042\u5206\u534a\u3060\u3063\u305f\u304c\u3001CPU\u3060\u3051\u3067\u8a08\u7b97\u3059\u308b\u5834\u5408\u306f\u305d\u306e\u4f55\u500d\u3082\u304b\u304b\u308b\u3053\u3068\u304c\u3042\u308b<\/td><\/tr><tr><td>MPS \u3067\u52d5\u304b\u3059\u3068\u30a8\u30e9\u30fc\u304c\u51fa\u308b<\/td><td>\u5b9f\u884c\u524d\u306b\u30bf\u30fc\u30df\u30ca\u30eb\u3067 <code>export PYTORCH_ENABLE_MPS_FALLBACK=1<\/code> \u3092\u6253\u3064\uff08MPS\u304c\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044\u8a08\u7b97\u3060\u3051CPU\u3067\u884c\u3046\uff09<\/td><\/tr><tr><td>\u6b63\u89e3\u7387\u304c50%\u524d\u5f8c\u306e\u307e\u307e<\/td><td>\u30c7\u30fc\u30bf\u3092\u30b7\u30e3\u30c3\u30d5\u30eb\u3057\u305f\u304b\u78ba\u304b\u3081\u308b\uff085. \u306e\u300c\u4ef6\u6570\u3092\u6e1b\u3089\u3059\u300d\uff09<\/td><\/tr><tr><td>\u9577\u3044\u30ec\u30d3\u30e5\u30fc\u306e\u5224\u5b9a\u304c\u305a\u308c\u308b<\/td><td>\u5f8c\u534a\u3092\u5207\u308a\u6368\u3066\u3066\u3044\u308b\u305f\u3081\u3002<code>MAX_LENGTH<\/code> \u3092256\u301c512\u306b\u5897\u3084\u3059\uff08\u8a08\u7b97\u306f\u9045\u304f\u306a\u308b\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">11. \u307e\u3068\u3081\u3068\u6b21\u306e\u4e00\u6b69<\/h2>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u6b21\u306e\u3053\u3068\u3092\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>BERT\u304c <strong>\u524d\u5f8c\u306e\u6587\u8108\u3092\u8aad\u3080<\/strong> \u30e2\u30c7\u30eb\u3067\u3042\u308b\u3053\u3068\u3001<strong>\u4e8b\u524d\u5b66\u7fd2\uff0b\u30d5\u30a1\u30a4\u30f3\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0<\/strong> \u306e2\u6bb5\u968e\u3067\u4f7f\u3046\u3053\u3068\u3092\u78ba\u8a8d\u3057\u305f<\/li>\n\n\n\n<li>\u8efd\u91cf\u7248\u306e <strong>DistilBERT<\/strong> \u3092\u3001IMDb \u306e\u6620\u753b\u30ec\u30d3\u30e5\u30fc2,000\u4ef6\u3067\u30d5\u30a1\u30a4\u30f3\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u3057\u305f<\/li>\n\n\n\n<li>GPU\u3092\u7a4d\u3093\u3060\u5c02\u7528\u306e\u30de\u30b7\u30f3\u304c\u306a\u304f\u3066\u3082\u3001\u624b\u5143\u306e Mac \u3067 <strong>\u5b66\u7fd2\u7d042\u5206\u534a\u3001\u6b63\u89e3\u738779.6%<\/strong> \u306e\u5206\u985e\u304c\u3067\u304d\u305f<\/li>\n\n\n\n<li>\u300cnot good\u300d\u306f\u6b63\u3057\u304f\u5426\u5b9a\u3068\u5224\u5b9a\u3067\u304d\u305f\u4e00\u65b9\u3067\u3001<strong>\u76ae\u8089<\/strong> \u306f\u307e\u3060\u898b\u629c\u3051\u306a\u304b\u3063\u305f<\/li>\n<\/ul>\n\n\n\n<p>\u6b21\u306e\u4e00\u6b69\u3068\u3057\u3066\u306f\u3001\u3053\u3093\u306a\u3053\u3068\u3092\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u672c\u5bb6\u306eBERT\u306b\u66ff\u3048\u308b<\/strong>\uff1a<code>MODEL_NAME<\/code> \u3092 <code>\"google-bert\/bert-base-uncased\"<\/code> \u306b\u5909\u3048\u308b\u3060\u3051\u3067\u52d5\u304d\u307e\u3059\uff08\u8a08\u7b97\u306f\u7d041.6\u500d\u304b\u304b\u308a\u307e\u3059\uff09<\/li>\n\n\n\n<li><strong>\u4ef6\u6570\u3084\u9577\u3055\u3092\u5897\u3084\u3059<\/strong>\uff1a<code>N_TRAIN<\/code> \u30925,000\u4ef6\u3001<code>MAX_LENGTH<\/code> \u3092256\u306b\u5897\u3084\u3059\u3068\u3001\u7cbe\u5ea6\u304c\u4e0a\u304c\u308b\u898b\u8fbc\u307f\u3067\u3059\u3002\u7279\u306b <code>MAX_LENGTH<\/code> \u3092\u5897\u3084\u3059\u3068\u3001\u5f8c\u534a\u3067\u8912\u3081\u3066\u3044\u308b\u30ec\u30d3\u30e5\u30fc\u3092\u8aad\u3081\u308b\u3088\u3046\u306b\u306a\u308a\u30018-2 \u3067\u898b\u305f\u300c\u80af\u5b9a\u3092\u5426\u5b9a\u3068\u53d6\u308a\u9055\u3048\u308b\u300d\u9593\u9055\u3044\u304c\u6e1b\u308b\u306f\u305a\u3067\u3059\uff08\u8a08\u7b97\u6642\u9593\u306f\u9577\u304f\u306a\u308a\u307e\u3059\uff09<\/li>\n\n\n\n<li><strong>3\u3064\u4ee5\u4e0a\u306b\u5206\u3051\u308b<\/strong>\uff1a\u30cb\u30e5\u30fc\u30b9\u30924\u3064\u306e\u30b8\u30e3\u30f3\u30eb\uff08\u4e16\u754c\u30fb\u30b9\u30dd\u30fc\u30c4\u30fb\u7d4c\u6e08\u30fb\u79d1\u5b66\u6280\u8853\uff09\u306b\u5206\u3051\u308b AG News \u306a\u3069\u3001\u591a\u30af\u30e9\u30b9\u306e\u5206\u985e\u306b\u6311\u6226\u3059\u308b\u3002<code>num_labels<\/code> \u3068 <code>id2label<\/code> \u3092\u5909\u3048\u308b\u3060\u3051\u3067\u3001\u30b3\u30fc\u30c9\u306e\u5f62\u306f\u307b\u307c\u540c\u3058\u3067\u3059<\/li>\n\n\n\n<li><strong>\u65e5\u672c\u8a9e\u3067\u8a66\u3059<\/strong>\uff1a\u65e5\u672c\u8a9e\u306b\u5bfe\u5fdc\u3057\u305fBERT\u3082\u516c\u958b\u3055\u308c\u3066\u3044\u307e\u3059<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u8003<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Devlin et al. (2018) &#8220;BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding&#8221;<\/li>\n\n\n\n<li>Sanh et al. (2019) &#8220;DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter&#8221;<\/li>\n\n\n\n<li>Maas et al. (2011) &#8220;Learning Word Vectors for Sentiment Analysis&#8221;\uff08IMDb \u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\uff09<\/li>\n\n\n\n<li>Hugging Face Transformers \u306e\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ed8\u9332\uff1a\u30b3\u30fc\u30c9\u5168\u6587\uff08<code>bert_imdb.py<\/code>\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"BERT\uff08DistilBERT\uff09\u3067 IMDb \u306e\u6620\u753b\u30ec\u30d3\u30e5\u30fc\u3092\u80af\u5b9a\u30fb\u5426\u5b9a\u306b\u5206\u985e\u3059\u308b\u3002\n\nGPU \u306e\u306a\u3044 Mac \u3067\u3082\u52d5\u304f\u3088\u3046\u3001\u4ef6\u6570\u3068\u6587\u306e\u9577\u3055\u3092\u7d5e\u3063\u3066\u3044\u308b\u3002\n\u4f7f\u3046\u306e\u306f\u516c\u958b\u30e2\u30c7\u30eb\u30fb\u516c\u958b\u30c7\u30fc\u30bf\u3060\u3051\u3002\u30ed\u30b0\u30a4\u30f3\u3084\u30c8\u30fc\u30af\u30f3\u306f\u4f7f\u308f\u306a\u3044\u3002\n\"\"\"\nimport os\n\n# Hugging Face \u3078\u306e\u5229\u7528\u72b6\u6cc1\u306e\u9001\u4fe1\u3092\u6b62\u3081\u308b\uff08\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u8aad\u307f\u8fbc\u3080\u524d\u306b\u8a2d\u5b9a\u3059\u308b\uff09\nos.environ&#091;\"HF_HUB_DISABLE_TELEMETRY\"] = \"1\"\n\nfrom collections import Counter\n\nimport numpy as np\nimport torch\nfrom datasets import load_dataset\nfrom sklearn.metrics import accuracy_score, confusion_matrix, f1_score\nfrom transformers import (\n    AutoModelForSequenceClassification,\n    AutoTokenizer,\n    DataCollatorWithPadding,\n    Trainer,\n    TrainingArguments,\n    set_seed,\n)\n\nset_seed(42)  # \u6bce\u56de\u540c\u3058\u7d50\u679c\u306b\u306a\u308b\u3088\u3046\u306b\u4e71\u6570\u3092\u56fa\u5b9a\u3059\u308b\n\nMODEL_NAME = \"distilbert\/distilbert-base-uncased\"\nMAX_LENGTH = 128   # 1\u4ef6\u3042\u305f\u308a\u306e\u30c8\u30fc\u30af\u30f3\u6570\u306e\u4e0a\u9650\nN_TRAIN = 2000     # \u5b66\u7fd2\u306b\u4f7f\u3046\u4ef6\u6570\nN_TEST = 500       # \u8a55\u4fa1\u306b\u4f7f\u3046\u4ef6\u6570\n\nprint(\"MPS:\", torch.backends.mps.is_available())\n\n# ---- 5. \u30c7\u30fc\u30bf\u3092\u898b\u3066\u307f\u308b ----\nimdb = load_dataset(\"stanfordnlp\/imdb\")\nprint(imdb)\n\n# IMDb \u306f\u30e9\u30d9\u30eb\u9806\u306b\u4e26\u3093\u3067\u3044\u308b\u306e\u3067\u3001\u30b7\u30e3\u30c3\u30d5\u30eb\u3057\u3066\u304b\u3089\u53d6\u308a\u51fa\u3059\ntrain_ds = imdb&#091;\"train\"].shuffle(seed=42).select(range(N_TRAIN))\ntest_ds = imdb&#091;\"test\"].shuffle(seed=42).select(range(N_TEST))\n\nprint(\"\u5b66\u7fd2\u7528\u306e\u30e9\u30d9\u30eb\u306e\u6570:\", Counter(train_ds&#091;\"label\"]))\nprint(\"\u8a55\u4fa1\u7528\u306e\u30e9\u30d9\u30eb\u306e\u6570:\", Counter(test_ds&#091;\"label\"]))\nprint(\"\u30ec\u30d3\u30e5\u30fc\u306e\u4f8b:\", train_ds&#091;0]&#091;\"text\"]&#091;:300])\nprint(\"\u30e9\u30d9\u30eb:\", train_ds&#091;0]&#091;\"label\"])\n\n# ---- 6. \u30c8\u30fc\u30af\u30f3\u5316 ----\ntokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n\nsample = tokenizer(\"This movie is not good.\")\nprint(sample)\nprint(tokenizer.convert_ids_to_tokens(sample&#091;\"input_ids\"]))\n\ndef tokenize(batch):\n    return tokenizer(batch&#091;\"text\"], truncation=True, max_length=MAX_LENGTH)\n\ntrain_tok = train_ds.map(tokenize, batched=True, remove_columns=&#091;\"text\"])\ntest_tok = test_ds.map(tokenize, batched=True, remove_columns=&#091;\"text\"])\ndata_collator = DataCollatorWithPadding(tokenizer=tokenizer)\n\n# ---- 7. \u30d5\u30a1\u30a4\u30f3\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0 ----\nid2label = {0: \"NEGATIVE\", 1: \"POSITIVE\"}\nlabel2id = {\"NEGATIVE\": 0, \"POSITIVE\": 1}\n\nmodel = AutoModelForSequenceClassification.from_pretrained(\n    MODEL_NAME,\n    num_labels=2,\n    id2label=id2label,\n    label2id=label2id,\n    use_safetensors=True,  # \u5b89\u5168\u306a\u4fdd\u5b58\u5f62\u5f0f\u306e\u30d5\u30a1\u30a4\u30eb\u3060\u3051\u3092\u8aad\u307f\u8fbc\u3080\n)\n\ndef compute_metrics(eval_pred):\n    logits, labels = eval_pred\n    preds = np.argmax(logits, axis=-1)\n    return {\n        \"accuracy\": accuracy_score(labels, preds),\n        \"f1\": f1_score(labels, preds),\n    }\n\nargs = TrainingArguments(\n    output_dir=\"out\",\n    num_train_epochs=1,               # \u30c7\u30fc\u30bf\u5168\u4f53\u3092\u4f55\u5468\u5b66\u7fd2\u3059\u308b\u304b\n    per_device_train_batch_size=16,   # \u4e00\u5ea6\u306b\u307e\u3068\u3081\u3066\u8a08\u7b97\u3059\u308b\u4ef6\u6570\n    per_device_eval_batch_size=32,\n    learning_rate=5e-5,               # 1\u56de\u306e\u5b66\u7fd2\u3067\u3069\u308c\u3060\u3051\u91cd\u307f\u3092\u52d5\u304b\u3059\u304b\n    weight_decay=0.01,                # \u5b66\u7fd2\u3057\u3059\u304e\uff08\u904e\u5b66\u7fd2\uff09\u3092\u6291\u3048\u308b\n    eval_strategy=\"epoch\",            # 1\u5468\u3054\u3068\u306b\u8a55\u4fa1\u3059\u308b\n    save_strategy=\"no\",               # \u9014\u4e2d\u7d4c\u904e\u306e\u30e2\u30c7\u30eb\u306f\u4fdd\u5b58\u3057\u306a\u3044\n    logging_steps=20,                 # 20\u56de\u3054\u3068\u306b\u640d\u5931\u3092\u8868\u793a\u3059\u308b\n    report_to=\"none\",                 # \u5916\u90e8\u306e\u8a18\u9332\u30b5\u30fc\u30d3\u30b9\u306b\u9001\u3089\u306a\u3044\n    seed=42,\n)\n\ntrainer = Trainer(\n    model=model,\n    args=args,\n    train_dataset=train_tok,\n    eval_dataset=test_tok,\n    data_collator=data_collator,\n    compute_metrics=compute_metrics,\n)\n\ntrainer.train()\n\n# ---- 8. \u8a55\u4fa1 ----\nmetrics = trainer.evaluate()\nprint(metrics)\n\npred = trainer.predict(test_tok)\npred_labels = np.argmax(pred.predictions, axis=-1)\ncm = confusion_matrix(pred.label_ids, pred_labels)\n\nprint(\"                \u4e88\u6e2c:NEGATIVE  \u4e88\u6e2c:POSITIVE\")\nprint(f\"\u6b63\u89e3:NEGATIVE    {cm&#091;0]&#091;0]:&gt;10}  {cm&#091;0]&#091;1]:&gt;12}\")\nprint(f\"\u6b63\u89e3:POSITIVE    {cm&#091;1]&#091;0]:&gt;10}  {cm&#091;1]&#091;1]:&gt;12}\")\n\n# ---- 9. \u81ea\u5206\u306e\u6587\u7ae0\u3067\u8a66\u3059 ----\ndef predict(texts):\n    model.eval()\n    inputs = tokenizer(\n        texts, truncation=True, max_length=MAX_LENGTH,\n        padding=True, return_tensors=\"pt\",\n    ).to(model.device)\n    with torch.no_grad():\n        probs = model(**inputs).logits.softmax(dim=-1)\n    for text, p in zip(texts, probs):\n        label = id2label&#091;int(p.argmax())]\n        print(f\"{label:8s} ({float(p.max()):.1%})  {text}\")\n\npredict(&#091;\n    \"This movie was absolutely wonderful. I loved every minute of it.\",\n    \"The plot was boring and the acting was terrible.\",\n    \"This movie is not good.\",\n    \"I expected to hate it, but it turned out to be great.\",\n    \"Oh great, another two hours of my life I will never get back.\",\n])\n<\/code><\/pre>\n\n\n\n<p>\u30bf\u30fc\u30df\u30ca\u30eb\u3067\u6b21\u3092\u6253\u3064\u3068\u52d5\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python bert_imdb.py\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u306f\u3058\u3081\u306b \u300c\u3053\u306e\u30e1\u30fc\u30eb\u306f\u8ff7\u60d1\u30e1\u30fc\u30eb\u304b\u300d\u300c\u3053\u306e\u30ec\u30d3\u30e5\u30fc\u306f\u8912\u3081\u3066\u3044\u308b\u304b\u3001\u3051\u306a\u3057\u3066\u3044\u308b\u304b\u300d\u300c\u3053\u306e\u30cb\u30e5\u30fc\u30b9\u306f\u30b9\u30dd\u30fc\u30c4\u304b\u3001\u7d4c\u6e08\u304b\u300d\u3002\u3053\u3046\u3057\u305f\u6587\u7ae0\u3092\u3044\u304f\u3064\u304b\u306e\u30b0\u30eb\u30fc\u30d7\u306b\u632f\u308a\u5206\u3051\u308b\u4f5c\u696d\u3092 \u30c6\u30ad\u30b9\u30c8\u5206\u985e \u3068\u547c\u3073\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u8eab\u306e\u56de\u308a [&hellip;]<\/p>\n","protected":false},"author":112,"featured_media":10397,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1251,1249],"tags":[331,39,27,1431,26,419],"class_list":["post-10389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-analysis","category-knowledge","tag-python","tag-39","tag-27","tag-1431","tag-26","tag-419"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/10389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/112"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=10389"}],"version-history":[{"count":5,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/10389\/revisions"}],"predecessor-version":[{"id":10402,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/10389\/revisions\/10402"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/10397"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=10389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=10389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=10389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}