Nối từ python

Xem các phiên từ Hội nghị chuyên đề WiML về các mô hình khuếch tán với KerasCV, ML trên thiết bị, v.v. Xem theo yêu cầu

  • TenorFlow
  • Tài nguyên
  • Chữ
  • Hướng dẫn

Chèn từ Sắp xếp ngăn nắp với các bộ sưu tập Lưu và phân loại nội dung dựa trên sở thích của bạn

Nối từ python
Xem trên TensorFlow. org
Nối từ python
Chạy trong Google Colab
Nối từ python
Xem nguồn trên GitHub
Nối từ python
Tải xuống sổ ghi chép

Hướng dẫn này chứa phần giới thiệu về nhúng từ. Bạn sẽ đào tạo cách nhúng từ của riêng mình bằng cách sử dụng mô hình Keras đơn giản cho nhiệm vụ phân loại cảm xúc, sau đó trực quan hóa chúng trong Máy chiếu nhúng (hiển thị trong hình ảnh bên dưới)

Nối từ python

Biểu diễn văn bản dưới dạng số

Các mô hình học máy lấy vectơ (mảng số) làm đầu vào. Khi làm việc với văn bản, điều đầu tiên bạn phải làm là đưa ra chiến lược chuyển đổi chuỗi thành số (hoặc "vectơ hóa" văn bản) trước khi đưa nó vào mô hình. Trong phần này, bạn sẽ xem xét ba chiến lược để làm như vậy

Mã hóa một lần nóng

Theo ý tưởng đầu tiên, bạn có thể mã hóa "một lần" từng từ trong vốn từ vựng của mình. Xét câu "Con mèo ngồi trên chiếu". Từ vựng (hoặc từ riêng) trong câu này là (cat, mat, on, sat, the). Để đại diện cho mỗi từ, bạn sẽ tạo một vectơ không có độ dài bằng từ vựng, sau đó đặt một vectơ vào chỉ mục tương ứng với từ đó. Cách tiếp cận này được thể hiện trong sơ đồ sau

Nối từ python

Để tạo một vectơ chứa mã hóa của câu, sau đó bạn có thể nối các vectơ one-hot cho mỗi từ

Điểm chính. Cách tiếp cận này không hiệu quả. Vectơ được mã hóa một lần là thưa thớt (có nghĩa là hầu hết các chỉ số đều bằng 0). Hãy tưởng tượng bạn có 10.000 từ trong kho từ vựng. Để mã hóa một lần mỗi từ, bạn sẽ tạo một vectơ trong đó 99. 99% các phần tử bằng 0.

Mã hóa mỗi từ bằng một số duy nhất

Cách tiếp cận thứ hai bạn có thể thử là mã hóa từng từ bằng một số duy nhất. Tiếp tục ví dụ trên, bạn có thể gán 1 cho "cat", 2 cho "mat", v.v. Sau đó, bạn có thể mã hóa câu "Con mèo ngồi trên chiếu" dưới dạng một vectơ dày đặc như [5, 1, 4, 3, 5, 2]. Cách làm này hiệu quả. Thay vì một vectơ thưa thớt, giờ đây bạn có một vectơ dày đặc (trong đó tất cả các phần tử đều đầy)

Tuy nhiên, có hai nhược điểm của phương pháp này

  • Mã hóa số nguyên là tùy ý (nó không nắm bắt bất kỳ mối quan hệ nào giữa các từ)

  • Mã hóa số nguyên có thể là một thách thức đối với một mô hình để diễn giải. Ví dụ: một bộ phân loại tuyến tính học một trọng số cho mỗi tính năng. Bởi vì không có mối quan hệ giữa sự giống nhau của hai từ bất kỳ và sự giống nhau của cách mã hóa chúng, nên sự kết hợp trọng số đặc trưng này không có ý nghĩa

Từ nhúng

Nhúng từ cung cấp cho chúng tôi cách sử dụng biểu diễn dày đặc, hiệu quả trong đó các từ tương tự có cách mã hóa tương tự. Điều quan trọng là bạn không phải chỉ định mã hóa này bằng tay. Phần nhúng là một vectơ dày đặc các giá trị dấu phẩy động (độ dài của vectơ là tham số bạn chỉ định). Thay vì chỉ định các giá trị cho quá trình nhúng theo cách thủ công, chúng là các tham số có thể huấn luyện (các trọng số được mô hình học trong quá trình huấn luyện, giống như cách một mô hình học các trọng số cho một lớp dày đặc). Người ta thường thấy các nhúng từ có 8 chiều (đối với tập dữ liệu nhỏ), lên đến 1024 chiều khi làm việc với tập dữ liệu lớn. Việc nhúng chiều cao hơn có thể nắm bắt các mối quan hệ chi tiết giữa các từ, nhưng cần nhiều dữ liệu hơn để tìm hiểu

Nối từ python

Trên đây là một sơ đồ cho một từ nhúng. Mỗi từ được biểu diễn dưới dạng một vectơ 4 chiều của các giá trị dấu phẩy động. Một cách khác để nghĩ về nhúng là "bảng tra cứu". Sau khi đã học được các trọng số này, bạn có thể mã hóa từng từ bằng cách tra cứu vectơ dày đặc mà từ đó tương ứng trong bảng

Cài đặt

import io
import os
import re
import shutil
import string
import tensorflow as tf

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D
from tensorflow.keras.layers import TextVectorization
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.

Tải xuống bộ dữ liệu IMDb

Bạn sẽ sử dụng Bộ dữ liệu đánh giá phim lớn thông qua hướng dẫn. Bạn sẽ đào tạo một mô hình phân loại tình cảm trên tập dữ liệu này và trong quá trình này, hãy tìm hiểu các cách nhúng từ đầu. Để đọc thêm về cách tải tập dữ liệu từ đầu, hãy xem phần Tải văn bản hướng dẫn

Tải xuống tập dữ liệu bằng tiện ích tệp Keras và xem các thư mục

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']

Hãy xem thư mục

Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
0. Nó có các thư mục
Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
1 và
Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
2 với các bài đánh giá phim được gắn nhãn tích cực và tiêu cực tương ứng. Bạn sẽ sử dụng các đánh giá từ các thư mục
Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
1 và
Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
2 để huấn luyện mô hình phân loại nhị phân

train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
0

Thư mục

Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
5 cũng có các thư mục bổ sung cần được xóa trước khi tạo tập dữ liệu huấn luyện

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
2

Tiếp theo, tạo một

Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
6 bằng cách sử dụng
Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
7. Bạn có thể đọc thêm về cách sử dụng tiện ích này trong hướng dẫn phân loại văn bản này

Sử dụng thư mục

Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
5 để tạo cả tập dữ liệu đào tạo và xác thực với tỷ lệ phân chia 20% để xác thực

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
6
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
7

Hãy xem một vài bài đánh giá phim và nhãn của chúng

Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
84125825/84125825 [==============================] - 3s 0us/step
['test', 'imdb.vocab', 'README', 'train', 'imdbEr.txt']
9 từ bộ dữ liệu xe lửa

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
9
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
0

Định cấu hình tập dữ liệu cho hiệu suất

Đây là hai phương pháp quan trọng bạn nên sử dụng khi tải dữ liệu để đảm bảo rằng I/O không bị chặn

train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
0 giữ dữ liệu trong bộ nhớ sau khi nó được tải ra khỏi đĩa. Điều này sẽ đảm bảo tập dữ liệu không trở thành nút cổ chai trong khi đào tạo mô hình của bạn. Nếu tập dữ liệu của bạn quá lớn để vừa với bộ nhớ, bạn cũng có thể sử dụng phương pháp này để tạo bộ nhớ đệm trên đĩa hoạt động hiệu quả, đọc hiệu quả hơn nhiều tệp nhỏ

train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
1 chồng chéo quá trình tiền xử lý dữ liệu và thực thi mô hình trong khi đào tạo

Bạn có thể tìm hiểu thêm về cả hai phương pháp, cũng như cách lưu trữ dữ liệu vào đĩa trong hướng dẫn hiệu suất dữ liệu

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
1

Sử dụng lớp Nhúng

Keras giúp dễ dàng sử dụng tính năng nhúng từ. Hãy nhìn vào lớp Nhúng

Lớp Nhúng có thể được hiểu là một bảng tra cứu ánh xạ từ các chỉ số nguyên (viết tắt của các từ cụ thể) sang các vectơ dày đặc (các phần nhúng của chúng). Chiều (hoặc chiều rộng) của quá trình nhúng là một tham số mà bạn có thể thử nghiệm để xem điều gì hoạt động tốt cho vấn đề của mình, giống như cách bạn sẽ thử nghiệm với số lượng nơ ron trong một Lớp dày đặc

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
2

Khi bạn tạo một lớp Nhúng, trọng số cho lớp nhúng được khởi tạo ngẫu nhiên (giống như bất kỳ lớp nào khác). Trong quá trình đào tạo, chúng dần dần được điều chỉnh thông qua lan truyền ngược. Sau khi được đào tạo, các nhúng từ đã học sẽ mã hóa đại khái những điểm tương đồng giữa các từ (như chúng đã được học cho vấn đề cụ thể mà mô hình của bạn được đào tạo)

Nếu bạn chuyển một số nguyên cho lớp nhúng, kết quả sẽ thay thế từng số nguyên bằng vectơ từ bảng nhúng

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
3____14

Đối với các vấn đề về văn bản hoặc chuỗi, lớp Nhúng lấy một tenxơ 2D gồm các số nguyên, có hình dạng

train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
2, trong đó mỗi mục nhập là một chuỗi các số nguyên. Nó có thể nhúng các chuỗi có độ dài thay đổi. Bạn có thể nạp vào lớp nhúng ở trên các lô có hình dạng
train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
3 (lô gồm 32 chuỗi có độ dài 10) hoặc
train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
4 (lô gồm 64 chuỗi có độ dài 15)

Tenxơ được trả về có nhiều trục hơn đầu vào, các vectơ nhúng được căn dọc theo trục cuối cùng mới. Truyền cho nó lô đầu vào

train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
5 và đầu ra là
train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
6

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
5
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
6

Khi được cung cấp một loạt các chuỗi làm đầu vào, một lớp nhúng sẽ trả về một tenxơ dấu phẩy động 3D, có hình dạng

train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
7. Để chuyển đổi từ dãy có độ dài thay đổi này sang một biểu diễn cố định, có nhiều cách tiếp cận tiêu chuẩn. Bạn có thể sử dụng lớp RNN, Chú ý hoặc tổng hợp trước khi chuyển nó sang lớp Mật độ cao. Hướng dẫn này sử dụng tổng hợp vì nó đơn giản nhất. Phân loại văn bản với hướng dẫn RNN là một bước tiếp theo tốt

tiền xử lý văn bản

Tiếp theo, xác định các bước tiền xử lý tập dữ liệu cần thiết cho mô hình phân loại cảm tính của bạn. Khởi tạo một lớp TextVectorization với các tham số mong muốn để vector hóa các bài đánh giá phim. Bạn có thể tìm hiểu thêm về cách sử dụng lớp này trong hướng dẫn Phân loại văn bản

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
7____18

Tạo mô hình phân loại

Sử dụng Keras Sequential API để xác định mô hình phân loại tình cảm. Trong trường hợp này, đó là mô hình kiểu "Túi từ liên tục"

  • Lớp
    train_dir = os.path.join(dataset_dir, 'train')
    os.listdir(train_dir)
    
    8 biến chuỗi thành chỉ số từ vựng. Bạn đã khởi tạo
    train_dir = os.path.join(dataset_dir, 'train')
    os.listdir(train_dir)
    
    9 dưới dạng lớp TextVectorization và xây dựng vốn từ vựng của nó bằng cách gọi
    2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
    
    00 trên
    2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
    
    01. Giờ đây, vectorize_layer có thể được sử dụng làm lớp đầu tiên của mô hình phân loại từ đầu đến cuối của bạn, đưa các chuỗi đã chuyển đổi vào lớp Nhúng
  • Lớp

    2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
    
    02 lấy từ vựng được mã hóa bằng số nguyên và tra cứu vectơ nhúng cho từng chỉ mục từ. Các vectơ này được học khi mô hình đào tạo. Các vectơ thêm một thứ nguyên vào mảng đầu ra. Kích thước kết quả là.
    2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
    
    03

  • Lớp

    2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
    
    04 trả về một vectơ đầu ra có độ dài cố định cho từng ví dụ bằng cách lấy trung bình trên thứ nguyên chuỗi. Điều này cho phép mô hình xử lý đầu vào có độ dài thay đổi theo cách đơn giản nhất có thể

  • Vectơ đầu ra có độ dài cố định được dẫn qua lớp (

    2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
    
    05) được kết nối đầy đủ với 16 đơn vị ẩn

  • Lớp cuối cùng được kết nối dày đặc với một nút đầu ra duy nhất

Thận trọng. Mô hình này không sử dụng mặt nạ, do đó, phần đệm bằng 0 được sử dụng như một phần của đầu vào và do đó độ dài của phần đệm có thể ảnh hưởng đến đầu ra. Để khắc phục điều này, hãy xem hướng dẫn tạo mặt nạ và đệm. ______19

Biên dịch và huấn luyện mô hình

Bạn sẽ sử dụng TensorBoard để trực quan hóa các số liệu bao gồm tổn thất và độ chính xác. Tạo một

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
06

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
0

Biên dịch và huấn luyện mô hình bằng cách sử dụng trình tối ưu hóa

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
07 và tổn thất
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
08

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
1
url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
2____23

Với cách tiếp cận này, mô hình đạt độ chính xác xác thực khoảng 78% (lưu ý rằng mô hình bị quá khớp vì độ chính xác đào tạo cao hơn)

Ghi chú. Kết quả của bạn có thể hơi khác một chút, tùy thuộc vào cách các trọng số được khởi tạo ngẫu nhiên trước khi huấn luyện lớp nhúng.

Bạn có thể xem phần tóm tắt mô hình để tìm hiểu thêm về từng lớp của mô hình

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
4
url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
5

Trực quan hóa các số liệu mô hình trong TensorBoard

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
6

Nối từ python

Truy xuất các từ nhúng được đào tạo và lưu chúng vào đĩa

Tiếp theo, truy xuất các từ nhúng đã học trong quá trình đào tạo. Các phần nhúng là trọng số của lớp Nhúng trong mô hình. Ma trận trọng số có hình dạng

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
09

Lấy trọng số từ mô hình bằng cách sử dụng

2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
20 và
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
21. Hàm
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
22 cung cấp từ vựng để tạo tệp siêu dữ liệu với một mã thông báo trên mỗi dòng

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
7

Ghi trọng lượng vào đĩa. Để sử dụng Máy chiếu nhúng, bạn sẽ tải lên hai tệp ở định dạng được phân tách bằng tab. một tệp vectơ (chứa phần nhúng) và tệp dữ liệu meta (chứa các từ)

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
8

Nếu bạn đang chạy hướng dẫn này trong Colaboratory, bạn có thể sử dụng đoạn mã sau để tải các tệp này xuống máy cục bộ của mình (hoặc sử dụng trình duyệt tệp, Xem -> Mục lục -> Trình duyệt tệp)

url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                  untar=True, cache_dir='.',
                                  cache_subdir='')

dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
os.listdir(dataset_dir)
9

Trực quan hóa các nhúng

Để trực quan hóa các phần nhúng, hãy tải chúng lên máy chiếu nhúng

Mở Máy chiếu nhúng (cái này cũng có thể chạy trong phiên bản TensorBoard cục bộ)

  • Nhấp vào "Tải dữ liệu"

  • Tải lên hai tệp bạn đã tạo ở trên.

    2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
    
    23 và
    2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
    2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
    
    24

Các phần nhúng bạn đã đào tạo sẽ được hiển thị. Bạn có thể tìm kiếm các từ để tìm hàng xóm gần nhất của họ. Ví dụ: hãy thử tìm kiếm "đẹp". Bạn có thể thấy những người hàng xóm thích "tuyệt vời"

Ghi chú. Về mặt thử nghiệm, bạn có thể tạo ra các nội dung nhúng dễ hiểu hơn bằng cách sử dụng một mô hình đơn giản hơn. Hãy thử xóa lớp
2022-12-14 12:16:56.601690: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601797: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-12-14 12:16:56.601808: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
25, đào tạo lại mô hình và trực quan hóa lại các phần nhúng.
Lưu ý. Thông thường, cần có một tập dữ liệu lớn hơn nhiều để huấn luyện các cách nhúng từ dễ hiểu hơn. Hướng dẫn này sử dụng một bộ dữ liệu IMDb nhỏ cho mục đích trình diễn.

Bước tiếp theo

Hướng dẫn này đã chỉ cho bạn cách đào tạo và trực quan hóa việc nhúng từ từ đầu trên một tập dữ liệu nhỏ