Created
October 12, 2015 08:17
-
-
Save stewartpark/187895beb89f0a1b3a54 to your computer and use it in GitHub Desktop.
Simple XOR learning with keras
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.core import Dense, Dropout, Activation | |
from keras.optimizers import SGD | |
import numpy as np | |
X = np.array([[0,0],[0,1],[1,0],[1,1]]) | |
y = np.array([[0],[1],[1],[0]]) | |
model = Sequential() | |
model.add(Dense(8, input_dim=2)) | |
model.add(Activation('tanh')) | |
model.add(Dense(1)) | |
model.add(Activation('sigmoid')) | |
sgd = SGD(lr=0.1) | |
model.compile(loss='binary_crossentropy', optimizer=sgd) | |
model.fit(X, y, show_accuracy=True, batch_size=1, nb_epoch=1000) | |
print(model.predict_proba(X)) | |
""" | |
[[ 0.0033028 ] | |
[ 0.99581173] | |
[ 0.99530098] | |
[ 0.00564186]] | |
""" |
@gauravkr0071 replace
model.compile(loss='mean_squared_error',optimizer='sgd')
by this
model.compile(loss='mean_squared_error',optimizer=sgd)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.initializers import RandomUniform
import numpy as np
x=np.array([[0.1,0.1,1],
[0.1,0.9,1],
[0.9,0.1,1],
[0.9,0.9,1]])
y=np.array([[0.1],[0.9],[0.9],[0.1]])
model= Sequential()
model.add(Dense(4,input_dim=3,activation="sigmoid",
bias_initializer=RandomUniform(minval=-1.0, maxval=1, seed=None)))
model.add(Dense(1,activation="sigmoid",bias_initializer=RandomUniform(minval=-1.0, maxval=1, seed=None)))
sgd=SGD(lr=0.01)
model.compile(loss='mean_squared_error',optimizer='sgd')
model.fit(x,y,epochs=5000,batch_size=1,verbose=1)
i am not geeting good result what i am doing wrong any idea