Basic Xor Neural Network - Monday Post #1

in #neuralnetwork7 years ago (edited)

it's not monday

It's Wednesday but I didn't post Monday, so...

quick update

I'm going to start writing posts every Monday afternoon. Follow if you like tech posts from AI to graphics cards.

Intro

I recently set myself the task of programming my own neural network that solves the Xor logic gate. If you don't know what either of those things are, Google is your friend. Check out @neurallearner for some background info. Talking to some professionals, I realized that Python is the best option for programming any machine learning code.

Snooping around the Internets, I put together some simple code using Keras in Python. You will need to use Python 3 for most neural network APIs. So, here we go. It's pretty self-explanatory, but comment any questions/comments you have. Also check out the Keras docs (https://keras.io/) for info on the functions that I used.

the basics

x = Example inputs for the xor logic gate
y = Example outputs

The next section is where the hidden layers and biases are initialized.
Finally, we train the model and predict outcomes for each example in x.

These are the basics of every neural network. The reason that I chose Keras is that it's really easy to set up the network's layers. Compared with barebones Tensorflow or any of the other APIs, Keras is really easy to use for a beginner.

code

import numpy as np

from keras.models import Sequential
from keras.layers import Dense, Activation

x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

model = Sequential()
model.add(Dense(2, input_shape=(2,)))
model.add(Activation('sigmoid'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])

model.fit(x, y, epochs=100000, batch_size=1, verbose=1)

print(model.predict(x, verbose=1))

I hope you enjoyed and can learn something from this! Stay tuned for more Monday posts.