Last active
August 8, 2017 12:16
-
-
Save AdityaSoni19031997/0777740ba6d810e1ae1d459b18a323ed to your computer and use it in GitHub Desktop.
Comparing Keras And Tensorflow(XOR Function Implementation)
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 | |
training_data = np.array([[0,0],[0,1],[1,0],[1,1]], "float32") | |
target_data = np.array([[0],[1],[1],[0]], "float32") | |
model = Sequential() | |
model.add(Dense(32, 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=1000, verbose=2) | |
print model.predict(training_data) |
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_1 = tf.Variable(tf.random_normal([hidden_nodes]), name="hidden_bias") | |
W_hidden_1 = tf.Variable(tf.random_normal([2, hidden_nodes]), name="hidden_weights") | |
hidden_1 = 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 Adam Optimizer for optimizing with a "stepsize" of 0.01 | |
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 range(0, 1000): | |
# 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]]}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment