{"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\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 read-only \"../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# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# CIFAR-10","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## 导入各种包","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import datasets, layers, optimizers\nimport matplotlib.pyplot as plt\n\nimport os","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 设置信息显示等级","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"2\"\n# os.environ['TF_CPP_MIN_LOG_LEVEL'] 信息显示的等级\n# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' 默认的显示等级，显示所有信息\n# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 只显示warning和Error\n# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 只显示Error","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 下载数据集","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"(x, y), (test_x, test_y) = datasets.cifar10.load_data()\n# 下载数据集\nprint(x.shape, y.shape)\n# 打印x与y的维度","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 数据处理","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def progross(x, y):\n    x = 2 * tf.cast(x, dtype=tf.float32) / 255. - 1.\n    # 将x的数据类型转换为float32\n    y = tf.one_hot(y, depth=10, dtype=tf.int32)\n    # 独热函数，转化为ont_hot型张量\n    y = tf.squeeze(y, axis=0)\n    # 删掉0维的1\n    return x, y\n\ndb_train = tf.data.Dataset.from_tensor_slices((x, y)).shuffle(10000)\n# 使用这个函数转化数据集时，按照传入数据的第一维度切分，生成相应的 dataset\n# 输入 x,y 进行切分一一对应\n# 打乱顺序\ndb_train = db_train.map(progross).batch(128)\n# 分成每个batch128个样本\n\ndb_test = tf.data.Dataset.from_tensor_slices((test_x, test_y))\ndb_test = db_test.map(progross).batch(128)\n\ntrain_inter = iter(db_train)\ntrain_next = next(train_inter)\nprint(train_next[0].shape, train_next[1][1])\n# 生成迭代器","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 编写自定义方法","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# 继承layers.Layer方法\nclass myDense(layers.Layer):\n    # 实现__init__()方法\n    def __init__(self, in_dim, out_dim):\n        # 调用母类中的__init__()\n        super(myDense, self).__init__()\n\n        self.kernel = self.add_weight('w', [in_dim, out_dim])\n        self.bias = self.add_weight('b', [out_dim])\n\n    # 实现call()方法\n    def call(self, inputs, training=None):\n        # 构建模型结构\n        out = inputs @ self.kernel + self.bias\n        return out","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 编写自定义模型","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# 继承keras.Model母类\nclass myModel(keras.Model):\n\n    def __init__(self):\n        # 调用母类中的__init__()方法\n        super(myModel, self).__init__()\n        # 调用自定义层类 并构建每一层的连接数\n        self.fc1 = myDense(32 * 32 * 3, 256)\n        self.fc2 = myDense(256, 128)\n        self.fc3 = myDense(128, 64)\n        self.fc4 = myDense(64, 32)\n        self.fc5 = myDense(32, 10)\n\n    # 构建一个五层的全连接网络\n    def call(self, inputs, training=None):\n        # 把输入模型中的图片进行打平操作\n        inputs = tf.reshape(inputs, [-1, 32 * 32 * 3])\n        # 把训练数据输入到自定义层中\n        x = self.fc1(inputs)\n        # 利用relu函数进行非线性激活操作\n        out = tf.nn.relu(x)\n        x = self.fc2(out)\n        out = tf.nn.relu(x)\n        x = self.fc3(out)\n        out = tf.nn.relu(x)\n        x = self.fc4(out)\n        out = tf.nn.relu(x)\n        x = self.fc5(out)\n        return x","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 使用模型训练数据","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"netWork = myModel()\nnetWork.build(input_shape=[None, 32 * 32 * 3])\nnetWork.summary()\n\nnetWork.compile(optimizer=optimizers.Adam(lr=1e-3),\n                loss=tf.losses.CategoricalCrossentropy(from_logits=True),\n                metrics=['accuracy']\n                )\nhistory = netWork.fit(db_train, epochs=20, validation_data=db_test, validation_freq=1)\n# netWork.evaluate(db_test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 成图像输出","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label='val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.4, 1])\nplt.legend(loc='lower right')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 全部代码实现","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import datasets, layers, optimizers\nimport matplotlib.pyplot as plt\n\nimport os\n\nos.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"2\"\n# os.environ['TF_CPP_MIN_LOG_LEVEL'] 信息显示的等级\n# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' 默认的显示等级，显示所有信息\n# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 只显示warning和Error\n# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 只显示Error\n\n(x, y), (test_x, test_y) = datasets.cifar10.load_data()\n# 下载数据集\nprint(x.shape, y.shape)\n# 打印x与y的维度\n\ndef progross(x, y):\n    x = 2 * tf.cast(x, dtype=tf.float32) / 255. - 1.\n    # 将x的数据类型转换为float32\n    y = tf.one_hot(y, depth=10, dtype=tf.int32)\n    # 独热函数，转化为ont_hot型张量\n    y = tf.squeeze(y, axis=0)\n    # 删掉0维的1\n    return x, y\n\n\ndb_train = tf.data.Dataset.from_tensor_slices((x, y)).shuffle(10000)\n# 使用这个函数转化数据集时，按照传入数据的第一维度切分，生成相应的 dataset\n# 输入 x,y 进行切分一一对应\n# 打乱顺序\ndb_train = db_train.map(progross).batch(128)\n# 分成每个batch128个样本\n\ndb_test = tf.data.Dataset.from_tensor_slices((test_x, test_y))\ndb_test = db_test.map(progross).batch(128)\n\ntrain_inter = iter(db_train)\ntrain_next = next(train_inter)\nprint(train_next[0].shape, train_next[1][1])\n# 生成迭代器\n\n# 继承layers.Layer方法\nclass myDense(layers.Layer):\n    # 实现__init__()方法\n    def __init__(self, in_dim, out_dim):\n        # 调用母类中的__init__()\n        super(myDense, self).__init__()\n\n        self.kernel = self.add_weight('w', [in_dim, out_dim])\n        self.bias = self.add_weight('b', [out_dim])\n\n    # 实现call()方法\n    def call(self, inputs, training=None):\n        # 构建模型结构\n        out = inputs @ self.kernel + self.bias\n        return out\n\n\n# 继承keras.Model母类\nclass myModel(keras.Model):\n\n    def __init__(self):\n        # 调用母类中的__init__()方法\n        super(myModel, self).__init__()\n        # 调用自定义层类 并构建每一层的连接数\n        self.fc1 = myDense(32 * 32 * 3, 256)\n        self.fc2 = myDense(256, 128)\n        self.fc3 = myDense(128, 64)\n        self.fc4 = myDense(64, 32)\n        self.fc5 = myDense(32, 10)\n\n    # 构建一个五层的全连接网络\n    def call(self, inputs, training=None):\n        # 把输入模型中的图片进行打平操作\n        inputs = tf.reshape(inputs, [-1, 32 * 32 * 3])\n        # 把训练数据输入到自定义层中\n        x = self.fc1(inputs)\n        # 利用relu函数进行非线性激活操作\n        out = tf.nn.relu(x)\n        x = self.fc2(out)\n        out = tf.nn.relu(x)\n        x = self.fc3(out)\n        out = tf.nn.relu(x)\n        x = self.fc4(out)\n        out = tf.nn.relu(x)\n        x = self.fc5(out)\n        return x\n\n\nnetWork = myModel()\nnetWork.build(input_shape=[None, 32 * 32 * 3])\nnetWork.summary()\n\nnetWork.compile(optimizer=optimizers.Adam(lr=1e-3),\n                loss=tf.losses.CategoricalCrossentropy(from_logits=True),\n                metrics=['accuracy']\n                )\nhistory = netWork.fit(db_train, epochs=20, validation_data=db_test, validation_freq=1)\n# netWork.evaluate(db_test)\n\nplt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label='val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.4, 1])\nplt.legend(loc='lower right')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"![](https://ftp.bmp.ovh/imgs/2020/08/5eb6138fecd84891.png)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"![](https://ftp.bmp.ovh/imgs/2020/08/3c8cce74a4c6b53f.png)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"![](https://ftp.bmp.ovh/imgs/2020/08/4b3ff5903ecc4b0f.png)","execution_count":null}],"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}