{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from __future__ import print_function\nimport keras\nfrom keras.datasets import cifar10\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Activation, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nimport os\n\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# The data, split between train and test sets:\n(x_train, y_label_train), (x_test, y_label_test) = cifar10.load_data()  ##利用keras里头的包就有资料\nprint('x_train shape:', x_train.shape)       ##查看维度\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')\n\n# Normalize the data. Before we need to connvert data type to float for computation.\nx_train = x_train.astype('float32')     ##转换字符类型\nx_test = x_test.astype('float32')\nx_train /= 255                          ##压缩到0-1之间\nx_test /= 255\n\nfrom keras.utils import np_utils \ny_label_train_onehot = np_utils.to_categorical(y_label_train)   ##标签分类问题 变成one_hot的形式\ny_label_test_onehot = np_utils.to_categorical(y_label_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"##用keras.models 模块搭建网络 方便快速!!\n\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32,input_shape = (32,32,3),kernel_size=(3,3),padding = 'same',activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(filters = 32,kernel_size=(3,3),padding = 'same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 64,kernel_size=(3,3),padding = 'same',activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(filters = 64,kernel_size=(3,3),padding = 'same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 128,kernel_size=(3,3),padding = 'same',activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(filters = 128,kernel_size=(3,3),padding = 'same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dropout(0.2))\nmodel.add(Dense(2500,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1500,activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation='softmax'))\n\n##定义模型训练!!\nmodel.compile(loss = 'categorical_crossentropy'\n             ,optimizer = 'adam',metrics = ['accuracy'])\n\n##查看模型!!\nmodel.summary()\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"##训练模型\nhistory = model.fit(x_train,y_label_train_onehot,validation_split=0.2,epochs = 5,batch_size=300,verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"##模型评估\nscores = model.evaluate(x_test,y_label_test_onehot,verbose = 0)\nscores[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_learning_curves(history):\n    pd.DataFrame(history.history).plot(figsize=(8, 5))\n    plt.grid(True)\n    plt.gca().set_ylim(0, 1)\n    plt.show()\n\nplot_learning_curves(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## 花太长时间计算了 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}