Created
December 31, 2017 21:10
-
-
Save mubaris/b39f9a5e4c8f442665d88e81961ae5a0 to your computer and use it in GitHub Desktop.
XOR Neural Network Keras
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from keras.models import Sequential | |
| from keras.layers.core import Dense | |
| # the four different states of the XOR gate | |
| training_data = np.array([[0,0],[0,1],[1,0],[1,1]], "float32") | |
| # the four expected results in the same order | |
| target_data = np.array([[0],[1],[1],[0]], "float32") | |
| model = Sequential() | |
| model.add(Dense(16, input_dim=2, activation='relu')) | |
| model.add(Dense(1, activation='sigmoid')) | |
| model.compile(loss='mean_squared_error', | |
| optimizer='adam', | |
| metrics=['binary_accuracy']) | |
| model.fit(training_data, target_data, nb_epoch=500, verbose=2) | |
| print model.predict(training_data).round() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment