-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples of Boston housing experiment
- Loading branch information
yanjun
committed
Mar 10, 2020
1 parent
303c7fc
commit bbc1639
Showing
4 changed files
with
1,343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 26, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import tensorflow.keras as keras\n", | ||
"from tensorflow.keras.models import Sequential\n", | ||
"from tensorflow.keras.layers import Dense, Dropout, Flatten\n", | ||
"from tensorflow.keras.layers import Conv2D, MaxPooling2D" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 27, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#生成虚拟数据, 标准数据集需要GPU,100张100*100RGB数据" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 28, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# 生成虚拟数据\n", | ||
"x_train = np.random.random((100, 100, 100, 3))\n", | ||
"y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)\n", | ||
"x_test = np.random.random((20, 100, 100, 3))\n", | ||
"y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 29, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#采用VGG结构,论文见https://arxiv.org/abs/1409.1556\" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 30, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model = Sequential()\n", | ||
"# 输入: 3 通道 100x100 像素图像 -> (100, 100, 3) 张量。\n", | ||
"# 使用 32 个大小为 3x3 的卷积滤波器。\n", | ||
"model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))\n", | ||
"model.add(Conv2D(32, (3, 3), activation='relu'))\n", | ||
"model.add(MaxPooling2D(pool_size=(2, 2)))\n", | ||
"model.add(Dropout(0.25))\n", | ||
"\n", | ||
"model.add(Conv2D(64, (3, 3), activation='relu'))\n", | ||
"model.add(Conv2D(64, (3, 3), activation='relu'))\n", | ||
"model.add(MaxPooling2D(pool_size=(2, 2)))\n", | ||
"model.add(Dropout(0.25))\n", | ||
"\n", | ||
"model.add(Flatten())\n", | ||
"model.add(Dense(256, activation='relu'))\n", | ||
"model.add(Dropout(0.5))\n", | ||
"model.add(Dense(10, activation='softmax'))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 31, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from tensorflow.keras.optimizers import SGD" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 32, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#带动量的随机梯度下降法进行优化,需要Compile,是因为Keras是静态图,在实际运行的时候进行编译,keras对优化器的说明见https://keras.io/zh/optimizers/" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 33, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n", | ||
"model.compile(loss='categorical_crossentropy', optimizer=sgd)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 34, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#训练10个epocch,只看loss" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 35, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Train on 100 samples\n", | ||
"Epoch 1/10\n", | ||
"100/100 [==============================] - 5s 54ms/sample - loss: 2.3074\n", | ||
"Epoch 2/10\n", | ||
"100/100 [==============================] - 2s 20ms/sample - loss: 2.3743\n", | ||
"Epoch 3/10\n", | ||
"100/100 [==============================] - 2s 19ms/sample - loss: 2.2947\n", | ||
"Epoch 4/10\n", | ||
"100/100 [==============================] - 2s 20ms/sample - loss: 2.2786\n", | ||
"Epoch 5/10\n", | ||
"100/100 [==============================] - 2s 20ms/sample - loss: 2.2792\n", | ||
"Epoch 6/10\n", | ||
"100/100 [==============================] - 2s 19ms/sample - loss: 2.3000\n", | ||
"Epoch 7/10\n", | ||
"100/100 [==============================] - 2s 19ms/sample - loss: 2.2852\n", | ||
"Epoch 8/10\n", | ||
"100/100 [==============================] - 2s 20ms/sample - loss: 2.2815\n", | ||
"Epoch 9/10\n", | ||
"100/100 [==============================] - 2s 20ms/sample - loss: 2.2767\n", | ||
"Epoch 10/10\n", | ||
"100/100 [==============================] - 2s 20ms/sample - loss: 2.2814\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<tensorflow.python.keras.callbacks.History at 0x69233c8d0>" | ||
] | ||
}, | ||
"execution_count": 35, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"model.fit(x_train, y_train, batch_size=32, epochs=10)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#测试集验证一下,另一个MNIST数据集需要同学自己在百度或谷歌的深度学习框架上训练" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 36, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"20/1 [========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================] - 0s 19ms/sample - loss: 2.3132\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"score = model.evaluate(x_test, y_test, batch_size=32)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.