Recurrent Neural Networks (RNN's) voor taalmodellen met Keras
David Cecchini
Data Scientist
Toepassingen van tekstclassificatie:
Wat verandert van binair naar multiklassen:
yVorm van de outputvariabele y:
# Example: num_classes = 3
y[0] = [0, 1, 0]
y.shape = (N, num_classes)
Aantal units in de outputlaag:
# Output layer
model.add(Dense(num_classes))

Activatiefunctie in de outputlaag:
softmax geeft de kans voor elke klasse# Output layer
model.add(Dense(num_classes, activation="softmax"))
Verliesfunctie:
# Compile the model
model.compile(loss='categorical_crossentropy')
y = ["sports", "economy", "data_science", "sports", "finance"] # Omzetten naar pandas Series-object y_series = pd.Series(y, dtype="category")# Print de categoriecodes print(y_series.cat.codes)
0 3
1 1
2 0
3 3
4 2
from tensorflow.keras.utils import to_categoricaly = np.array([0, 1, 2]) # Omzetten naar categorisch y_prep = to_categorical(y) print(y_prep)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Recurrent Neural Networks (RNN's) voor taalmodellen met Keras