Mạng nơ-ron hồi quy (RNN) cho Mô hình ngôn ngữ với Keras
David Cecchini
Data Scientist
API cấp cao
Chạy trên TensorFlow 2
Dễ cài đặt và dùng
$pip install tensorflow
Thử nghiệm nhanh:
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
keras.models.Sequential

keras.models.Model

LSTMGRUDenseDropoutEmbeddingBidirectionalkeras.preprocessing.sequence.pad_sequences(texts, maxlen=3)

Nhiều bộ dữ liệu hữu ích
Và còn nữa!
Xem danh sách đầy đủ và ví dụ tại tài liệu keras
# Import required modules
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Instantiate the model class
model = Sequential()
# Add the layers
model.add(Dense(64, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
Phương thức .fit() huấn luyện mô hình trên tập train
model.fit(X_train, y_train, epochs=10, batch_size=32)
epochs: số lần cập nhật trọng sốbatch_size: kích thước dữ liệu mỗi bướcĐánh giá mô hình:
model.evaluate(X_test, y_test)
[0.3916562925338745, 0.89324]
Dự đoán trên dữ liệu mới:
model.predict(new_data)
array([[0.91483957],[0.47130653]], dtype=float32)
# Build and compile the model model = Sequential()model.add(Embedding(10000, 128)) model.add(LSTM(128, dropout=0.2)) model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Training
model.fit(x_train, y_train, epochs=5)
# Evaluation
score, acc = model.evaluate(x_test, y_test)
Mạng nơ-ron hồi quy (RNN) cho Mô hình ngôn ngữ với Keras