Created
January 4, 2018 16:33
-
-
Save kstrempel/d991e31525203f1eb0210001e288a115 to your computer and use it in GitHub Desktop.
A very simple Keras XOR Modell and training and testing
This file contains 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
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.optimizers import SGD | |
X = [[0.0, 0.0], | |
[0.0, 1.0], | |
[1.0, 0.0], | |
[0.0, 0.0]] | |
Y = [[0.0], | |
[1.0], | |
[1.0], | |
[0.0]] | |
sgd = SGD(lr=0.1) | |
def create_network(): | |
model = Sequential() | |
model.add(Dense(8, input_dim=2, activation='tanh')) | |
model.add(Dense(1, activation='sigmoid')) | |
model.compile(loss='binary_crossentropy', optimizer=sgd) | |
return model | |
def train(model): | |
model.fit(X, Y, epochs=1000, batch_size=4) | |
return model | |
def predict(model): | |
return model.predict(X) | |
if __name__ == "__main__": | |
model = create_network() | |
before_training = predict(model) | |
train(model) | |
print("Before Training") | |
print(before_training) | |
print("After Training") | |
print(predict(model)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment