-
-
Save ricgu8086/0ba44ce3aab19ec50425383a4d778b50 to your computer and use it in GitHub Desktop.
Comparing XOR between tensorflow and 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
import numpy as np | |
from keras.models import Sequential | |
from keras.layers.core import Activation, Dense | |
from keras.optimizers import SGD | |
X = np.array([[0,0],[0,1],[1,0],[1,1]], "float32") | |
y = np.array([[0],[1],[1],[0]], "float32") | |
model = Sequential() | |
model.add(Dense(2, input_dim=2, activation='sigmoid')) | |
model.add(Dense(1, activation='sigmoid')) | |
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) | |
model.compile(loss='mean_squared_error', optimizer=sgd) | |
history = model.fit(X, y, nb_epoch=10000, batch_size=4, verbose=0) | |
print model.predict(X) |
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
import tensorflow as tf | |
input_data = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]] # XOR input | |
output_data = [[0.], [1.], [1.], [0.]] # XOR output | |
n_input = tf.placeholder(tf.float32, shape=[None, 2], name="n_input") | |
n_output = tf.placeholder(tf.float32, shape=[None, 1], name="n_output") | |
hidden_nodes = 5 | |
b_hidden = tf.Variable(tf.random_normal([hidden_nodes]), name="hidden_bias") | |
W_hidden = tf.Variable(tf.random_normal([2, hidden_nodes]), name="hidden_weights") | |
hidden = tf.sigmoid(tf.matmul(n_input, W_hidden) + b_hidden) | |
W_output = tf.Variable(tf.random_normal([hidden_nodes, 1]), name="output_weights") # output layer's weight matrix | |
output = tf.sigmoid(tf.matmul(hidden, W_output)) # calc output layer's activation | |
cross_entropy = tf.square(n_output - output) # simpler, but also works | |
loss = tf.reduce_mean(cross_entropy) # mean the cross_entropy | |
optimizer = tf.train.AdamOptimizer(0.01) # take a gradient descent for optimizing with a "stepsize" of 0.1 | |
train = optimizer.minimize(loss) # let the optimizer train | |
init = tf.initialize_all_variables() | |
sess = tf.Session() # create the session and therefore the graph | |
sess.run(init) # initialize all variables | |
for epoch in xrange(0, 2001): | |
# run the training operation | |
cvalues = sess.run([train, loss, W_hidden, b_hidden, W_output], | |
feed_dict={n_input: input_data, n_output: output_data}) | |
if epoch % 200 == 0: | |
print("") | |
print("step: {:>3}".format(epoch)) | |
print("loss: {}".format(cvalues[1])) | |
print("") | |
print("input: {} | output: {}".format(input_data[0], sess.run(output, feed_dict={n_input: [input_data[0]]}))) | |
print("input: {} | output: {}".format(input_data[1], sess.run(output, feed_dict={n_input: [input_data[1]]}))) | |
print("input: {} | output: {}".format(input_data[2], sess.run(output, feed_dict={n_input: [input_data[2]]}))) | |
print("input: {} | output: {}".format(input_data[3], sess.run(output, feed_dict={n_input: [input_data[3]]}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment