python-tensorflowHow can I use Python and TensorFlow to create an XOR gate?
This can be done by creating a neural network with two input neurons, one output neuron and one hidden layer with two neurons. With this structure, we can create an XOR gate using TensorFlow and Python.
import tensorflow as tf
# Inputs
x = tf.placeholder(tf.float32, shape=[4,2], name='x')
# Outputs
y_ = tf.placeholder(tf.float32, shape=[4,1], name='y_')
# Weights and biases
W_h = tf.Variable(tf.random_uniform([2,2], -1, 1), name="W_h")
b_h = tf.Variable(tf.zeros([2]), name="b_h")
W_o = tf.Variable(tf.random_uniform([2,1], -1, 1), name="W_o")
b_o = tf.Variable(tf.zeros([1]), name="b_o")
# Hidden Layer
h = tf.sigmoid(tf.matmul(x, W_h) + b_h)
# Output Layer
y = tf.sigmoid(tf.matmul(h, W_o) + b_o)
# Cost Function
cost = tf.reduce_mean(( (y_ * tf.log(y)) +
((1 - y_) * tf.log(1.0 - y)) ) * -1)
# Optimizer
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
# Training
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
# Training Data
XOR_X = [[0,0],[0,1],[1,0],[1,1]]
XOR_Y = [[0],[1],[1],[0]]
# Run
for i in range(100000):
sess.run(train_step, feed_dict={x: XOR_X, y_: XOR_Y})
# Print
print('Output:')
print(sess.run(y, feed_dict={x: XOR_X}))
Output example
[[0.01991707]
[0.980087 ]
[0.97948706]
[0.01991817]]
Code explanation
- Importing the TensorFlow library:
import tensorflow as tf
- Creating the placeholders for inputs and outputs:
x = tf.placeholder(tf.float32, shape=[4,2], name='x')
andy_ = tf.placeholder(tf.float32, shape=[4,1], name='y_')
- Creating the weights and biases for the hidden and output layers:
W_h = tf.Variable(tf.random_uniform([2,2], -1, 1), name="W_h")
,b_h = tf.Variable(tf.zeros([2]), name="b_h")
,W_o = tf.Variable(tf.random_uniform([2,1], -1, 1), name="W_o")
andb_o = tf.Variable(tf.zeros([1]), name="b_o")
- Computing the output of the hidden layer:
h = tf.sigmoid(tf.matmul(x, W_h) + b_h)
- Computing the output of the output layer:
y = tf.sigmoid(tf.matmul(h, W_o) + b_o)
- Computing the cost function:
cost = tf.reduce_mean(( (y_ * tf.log(y)) + ((1 - y_) * tf.log(1.0 - y)) ) * -1)
- Creating the optimizer:
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
- Initializing the variables:
init = tf.global_variables_initializer()
- Creating the training data:
XOR_X = [[0,0],[0,1],[1,0],[1,1]]
andXOR_Y = [[0],[1],[1],[0]]
- Running the training loop:
for i in range(100000): sess.run(train_step, feed_dict={x: XOR_X, y_: XOR_Y})
- Printing the output:
print(sess.run(y, feed_dict={x: XOR_X}))
Helpful links
More of Python Tensorflow
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How can I use Python TensorFlow in W3Schools?
- How can I use Python and TensorFlow to implement YOLO object detection?
- How can I use YOLOv3 with Python and TensorFlow?
- How do I use TensorFlow 1.x with Python?
- How can I use Tensorflow 1.x with Python 3.8?
- How can I use TensorFlow with Python 3.11?
- How can I use Python and TensorFlow to implement YOLOv4?
See more codes...