Skip to content

Instantly share code, notes, and snippets.

@ypwhs
Created July 10, 2017 02:34
Show Gist options
  • Save ypwhs/2e4653de26af7c674e00d19896db4cef to your computer and use it in GitHub Desktop.
Save ypwhs/2e4653de26af7c674e00d19896db4cef to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"from keras.layers import *\n",
"from keras.models import *\n",
"from keras.optimizers import *\n",
"from keras.utils.np_utils import to_categorical\n",
"from keras.datasets.mnist import load_data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"(X_train, y_train), (X_test, y_test) = load_data()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"y_train = to_categorical(y_train)\n",
"y_test = to_categorical(y_test)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Flatten(input_shape=X_train.shape[1:]))\n",
"model.add(Dense(512, kernel_initializer='zeros', bias_initializer='zeros', activation='sigmoid'))\n",
"model.add(Dense(10, kernel_initializer='zeros', bias_initializer='zeros', activation='softmax'))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 60000 samples, validate on 10000 samples\n",
"Epoch 1/5\n",
"60000/60000 [==============================] - 4s - loss: 1.9631 - acc: 0.2138 - val_loss: 1.9858 - val_acc: 0.1893\n",
"Epoch 2/5\n",
"60000/60000 [==============================] - 3s - loss: 1.8677 - acc: 0.2132 - val_loss: 1.8357 - val_acc: 0.2040\n",
"Epoch 3/5\n",
"60000/60000 [==============================] - 3s - loss: 1.8136 - acc: 0.2019 - val_loss: 1.7956 - val_acc: 0.2155\n",
"Epoch 4/5\n",
"60000/60000 [==============================] - 3s - loss: 1.7998 - acc: 0.1982 - val_loss: 1.8179 - val_acc: 0.2146\n",
"Epoch 5/5\n",
"60000/60000 [==============================] - 3s - loss: 1.7939 - acc: 0.2026 - val_loss: 1.7971 - val_acc: 0.1957\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x1219e3f90>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.compile(optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True), \n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy'])\n",
"\n",
"model.fit(X_train, y_train, batch_size=128, epochs=5, validation_data=(X_test, y_test))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"[array([[ 0., 0., 0., ..., 0., 0., 0.],\n",
" [ 0., 0., 0., ..., 0., 0., 0.],\n",
" [ 0., 0., 0., ..., 0., 0., 0.],\n",
" ..., \n",
" [ 0., 0., 0., ..., 0., 0., 0.],\n",
" [ 0., 0., 0., ..., 0., 0., 0.],\n",
" [ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32),\n",
" array([ 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311, 0.00023311, 0.00023311, 0.00023311,\n",
" 0.00023311, 0.00023311], dtype=float32)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.layers[1].get_weights()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment