<jupyter_start><jupyter_text>Sequential Model<jupyter_code>from keras.models import Sequential
# create the sequential model
model = Sequential()<jupyter_output><empty_output><jupyter_text>The keras.models.Sequential class is a wrapper for the neural network model that treats the network as a sequence of layers. It implements the Keras model interface with common methods like compile(), fit(), and evaluate() that are used to train and run the model.  Layers<jupyter_code>import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Activation<jupyter_output><empty_output><jupyter_text>The Keras Layer class provides a common interface for a variety of standard neural network layers. There are fully connected layers, max pool layers, activation layers, and more. You can add a layer to a model using the model's add() method.<jupyter_code># X has shape (num_rows, num_cols), where the training data are stored
# as row vectors
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)

# y must have an output vector for each input vector
y = np.array([[0], [0], [0], [1]], dtype=np.float32)
model = Sequential()

# input layer with 32 nodes
model.add(Dense(32, input_dim=X.shape[1]))

# output layer
model.add(Dense(1))

# softmax activation layer
model.add(Activation('sigmoid'))<jupyter_output><empty_output><jupyter_text>Once we have our model built, we need to compile it before it can be run. Compiling the Keras model calls the backend (tensorflow, theano, etc.) and binds the optimizer, loss function, and other parameters required before the model can be run on any input data.<jupyter_code>model.compile(loss="binary_crossentropy", 
              optimizer="adam", metrics = ["accuracy"])
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 32)                96        
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 33        
_________________________________________________________________
activation_1 (Activation)    (None, 1)                 0         
=================================================================
Total params: 129
Trainable params: 129
Non-trainable params: 0
_________________________________________________________________<jupyter_text>The model is trained with the fit() method, through the following command that specifies the number of training epochs and the message level (how much information we want displayed on the screen during training).<jupyter_code>model.fit(X, y, epochs=1000, verbose=0)
model.evaluate(X,y)<jupyter_output>4/4 [==============================] - 0s<jupyter_text>XOR MLP<jupyter_code>import numpy as np
from keras.utils import np_utils
import tensorflow as tf
# Using TensorFlow 1.0.0; use tf.python_io in later versions
tf.python_io.control_flow_ops = tf

# Set random seed
np.random.seed(42)

# Our data
X = np.array([[0,0],[0,1],[1,0],[1,1]]).astype('float32')
y = np.array([[0],[1],[1],[0]]).astype('float32')

# Initial Setup for Keras
from keras.models import Sequential
from keras.layers.core import Dense, Activation

# Building the model
xor = Sequential()

# Add required layers
# xor.add()
xor.add(Dense(8, input_dim=X.shape[1]))
xor.add(Activation("tanh"))
xor.add(Dense(1))
xor.add(Activation("sigmoid"))

# Specify loss as "binary_crossentropy", optimizer as "adam",
# and add the accuracy metric
# xor.compile()
xor.compile(loss="binary_crossentropy", 
            optimizer="adam", metrics=["accuracy"])

# Uncomment this line to print the model architecture
xor.summary()

# Fitting the model
history = xor.fit(X, y, epochs=500, verbose=0)

# Scoring the model
score = xor.evaluate(X, y)
print("\nAccuracy: ", score[-1])

# Checking the predictions
print("\nPredictions:")
print(xor.predict_proba(X))<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_5 (Dense)              (None, 8)                 24        
_________________________________________________________________
activation_4 (Activation)    (None, 8)                 0         
_________________________________________________________________
dense_6 (Dense)              (None, 1)                 9         
_________________________________________________________________
activation_5 (Activation)    (None, 1)                 0         
=================================================================
Total params: 33
Trainable params: 33
Non-trainable params: 0
_________________________________________________________________
4/4 [==============================] - 0s

Accuracy:  1.0

Predictions:
4/4 [==============================] - 0s
[[ 0.26272935]
 [ 0.65377146]
 [[...]
{'fraction_non_alphanumeric': 0.15926219754065846, 'fraction_numerical': 0.024196747322491077, 'mean_word_length': 4.63463687150838, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '158599', 'license': 'MIT', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1305, 'n_words': 427, 'path': 'building_NN_keras/1_Building_a_Neural_Network_in_Keras.ipynb', 'repo_name': 'wszjzhang/DL_ND', 'size': '5042'}