<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 A "Hello world" in Keras
Keras
Step1:  Generate data
We generate a very simple dataset
Step2:  Split the data into training and test set
No validation set since we won't tune any hyperparamers today.
Step3:  Create the model
Plain neural network with a single input, hidden and output layer.
The input size matches the number of our features. The output size matches the number of classes (due to one-hot encoding).
We chose 3 neurons in the hidden layer.
Step4:  Train the model
We train the model in 5 epochs with mini-batches of size 32 using plain SGD.
The progress is nicely printed on the console. A nice thing over theanets is that the progressbar is overwritten and not just appending each row, this saves visual space and avoids cluttering.
Step5:  Evaluate the model
Since we have multi-class classification problem the basic metric is accuracy.
The keras model allows to compute it for us. Otherwise we can grab for sklearn.
Also while using the model for predictions the progress is printed.
<ASSISTANT_TASK:>
Python Code:
%pylab inline
from keras.layers.core import Dense, Activation
from keras.models import Sequential
from keras.utils import np_utils
from sklearn.cross_validation import train_test_split
from sklearn.datasets.samples_generator import make_blobs
from sklearn.metrics import classification_report, confusion_matrix
Explanation: A "Hello world" in Keras
Keras: Theano-based Deep Learning library
This is just a minimal and very brief example of using Keras to provide a simple working setup that can be modified for more complex tasks.
This model's task is rather trivial - to classify a few almost linearly separable 2D gaussian blobs.
The setup is very similar to the previous Theanets "Hello world".
Install
This tutorial assumes you have the basic stuff prepared.
- Python (eg. anaconda with Python 3.x)
- numpy, matplotlib, ipython, sklearn
```
get theano (eg. the latest from github)
pip install git+git://github.com/Theano/Theano.git
pip install Keras
```
End of explanation
n_samples = 10000
n_classes = 3
n_features = 2
# centers - number of classes
# n_features - dimension of the data
X, y_int = make_blobs(n_samples=n_samples, centers=n_classes, n_features=n_features, \
    cluster_std=0.5, random_state=0)
# No need to convert the features and targets to the 32-bit format as in plain theano.
# labels need to be one-hot encoded (binary vector of size N for N classes)
y = np_utils.to_categorical(y_int, n_classes)
# visualize the data for better understanding
def plot_2d_blobs(dataset):
    X, y = dataset
    axis('equal')
    scatter(X[:, 0], X[:, 1], c=y, alpha=0.1, edgecolors='none')
plot_2d_blobs((X, y_int))
Explanation: Generate data
We generate a very simple dataset: three almost linearly separable gaussian blobs in 2D.
End of explanation
# split the data into training, validation and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
Explanation: Split the data into training and test set
No validation set since we won't tune any hyperparamers today.
End of explanation
# the model is just a sequence of transformations - layer weights, activations, etc.
model = Sequential()
# weights from input to hidden layer - linear transform
model.add(Dense(3, input_dim=n_features))
# basic non-linearity
model.add(Activation("tanh"))
# weights from hidden to output layer
model.add(Dense(n_classes))
# nonlinearity suitable for a classifier
model.add(Activation("softmax"))
# - loss function suitable for multi-class classification
# - plain stochastic gradient descent with mini-batches
model.compile(loss='categorical_crossentropy', optimizer='sgd')
Explanation: Create the model
Plain neural network with a single input, hidden and output layer.
The input size matches the number of our features. The output size matches the number of classes (due to one-hot encoding).
We chose 3 neurons in the hidden layer.
End of explanation
model.fit(X_train, y_train, nb_epoch=5, batch_size=32);
Explanation: Train the model
We train the model in 5 epochs with mini-batches of size 32 using plain SGD.
The progress is nicely printed on the console. A nice thing over theanets is that the progressbar is overwritten and not just appending each row, this saves visual space and avoids cluttering.
End of explanation
def evaluate_accuracy(X, y, label):
    _, accuracy = model.evaluate(X_train, y_train, show_accuracy=True)
    print('training accuracy:', 100 * accuracy, '%')
evaluate_accuracy(X_train, X_train, 'training')
evaluate_accuracy(X_test, X_test, 'test')
y_test_pred = model.predict_classes(X_test)
plot_2d_blobs((X_test, y_test_pred))
Explanation: Evaluate the model
Since we have multi-class classification problem the basic metric is accuracy.
The keras model allows to compute it for us. Otherwise we can grab for sklearn.
Also while using the model for predictions the progress is printed.
End of explanation
<END_TASK>