if index < max_words:
        embedding_vector = embeddings_index.get(word)
        if embedding_matrix is not None:
            embedding_matrix[index] = embedding_vector

# Build the model
model = models.Sequential()
model.add(layers.Embedding(max_words, embedding_dim, input_length=n_timesteps))
model.add(layers.Flatten())
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

# Load the pre-trained GloVe embedding in the model
model.layers[0].set_weights([embedding_matrix])
model.layers[0].trainable = False

# Compile
model.compile(optimizer=optimizers.RMSprop(1e-5),
              loss='binary_crossentropy',
              metrics=['acc'])
history = model.fit(
    X_train,
    y_train,
    epochs=10,
    batch_size=32,
    validation_data=(X_val, y_val),
)

# Visualization
plot_loss_and_accuracy(history)
Beispiel #2
0
#-------------------------------------------------------------------------------

# Modify label dimension for keras
y_2d = to_categorical(y, num_classes=2)

# Build network
model = lab8.keras_nn(n_input=X.shape[1], n_hidden=50, n_output=2)
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# Train network
history = model.fit(X, y_2d, epochs=500, batch_size=10)

# Plot loss and accuracy
utils.plot_loss_and_accuracy(history)
plt.show()

# Plot decision boundary
ax = plt.gca()
utils.plot_decision_boundary(ax,
                             X,
                             y,
                             model,
                             title='Keras Neural Network Moons',
                             multiclass=True)
plt.show()

#-------------------------------------------------------------------------------
# Part 7 - Clustering Dataset with Keras
#-------------------------------------------------------------------------------