In [1]:
# Multilayer Perceptron 10 inputs, 3 hidden layers with 10, 20, and 10 neurons, and an output layer with 1 output
from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
visible = Input(shape=(10,))
hidden1 = Dense(10, activation='relu')(visible)
hidden2 = Dense(20, activation='relu')(hidden1)
hidden3 = Dense(10, activation='relu')(hidden2)
output = Dense(1, activation='sigmoid')(hidden3)
model = Model(inputs=visible, outputs=output)
# summarize layers
model.summary()
# plot graph
plot_model(model, to_file='multilayer_perceptron_graph.png')
In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('multilayer_perceptron_graph.png')
imgplot = plt.imshow(img)
plt.show()
In [3]:
# Convolutional Neural Network
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
visible = Input(shape=(64,64,1))
conv1 = Conv2D(32, kernel_size=4, activation='relu')(visible)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(16, kernel_size=4, activation='relu')(pool1)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
hidden1 = Dense(10, activation='relu')(pool2)
output = Dense(1, activation='sigmoid')(hidden1)
model = Model(inputs=visible, outputs=output)
# summarize layers
model.summary()
# plot graph
plot_model(model, to_file='convolutional_neural_network.png')
In [4]:
img=mpimg.imread('convolutional_neural_network.png')
imgplot = plt.imshow(img)
plt.show()
In [5]:
# Recurrent Neural Network
from keras.layers.recurrent import LSTM
visible = Input(shape=(100,1))
hidden1 = LSTM(10)(visible)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(1, activation='sigmoid')(hidden2)
model = Model(inputs=visible, outputs=output)
# summarize layers
model.summary()
# plot graph
plot_model(model, to_file='recurrent_neural_network.png')
In [6]:
img=mpimg.imread('recurrent_neural_network.png')
imgplot = plt.imshow(img)
plt.show()
In [7]:
from keras.models import Sequential
from keras.layers import Activation
model = Sequential()
#model.add(Dense(5))
#layers = [Dense(5)]
#model = Sequential(layers)
model.add(Dense(5, input_dim=2))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(optimizer='sgd', loss='mean_squared_error', metrics=['accuracy'])
#algorithm = SGD(lr=0.1, momentum=0.3)
#model.compile(optimizer=algorithm, loss='mean_squared_error')
In [ ]:
history = model.fit(X, y, batch_size=10, epochs=100)
loss, accuracy = model.evaluate(X, y)
predictions = model.predict(X)
# Multi-Class
predictions = model.predict_classes(X)