{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "mnist=input_data.read_data_sets('MNIST_data/',one_hot=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(64, 10)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train,y_train=mnist.train.next_batch(64)\n",
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def weight_variable(shape):\n",
    "    initial = tf.truncated_normal(shape,stddev=0.1)\n",
    "    return tf.Variable(initial)\n",
    "\n",
    "def bias_variable(shape):\n",
    "\n",
    "    initial = tf.constant(0.1,shape=shape)\n",
    "    return tf.Variable(initial)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "回忆卷积步骤\n",
    "1、定义卷积核与bias\n",
    "2、上一层输出，卷积核，一起输入到卷积操作中，\n",
    "3、将结果加上bias\n",
    "4、返回结果\n",
    "\n",
    "'''\n",
    "\n",
    "def conv_2d(input_data,k_h=3,k_w=3,s_h=1,s_w=1,\n",
    "            output_channel=64,padding='SAME',name='conv_i'):\n",
    "    \n",
    "    input_data_channel=int(input_data.shape[-1])\n",
    "    # 这个可以理解为是方便显示用的\n",
    "    with tf.name_scope(name) as scope:\n",
    "        initial_W = tf.truncated_normal(shape=[k_h,k_w,\n",
    "                                               input_data_channel,output_channel],\n",
    "                                        stddev=0.1)\n",
    "        W=tf.Variable(initial_W,name=name+'_'+'weights')\n",
    "        \n",
    "        initial_b = tf.constant(0.1,shape=[output_channel])\n",
    "        b=tf.Variable(initial_b,name=name+'_'+'bias')\n",
    "        '''\n",
    "        strids表示步长的维度信息，定义如下：[batch, height, width, channels]维度上的步长\n",
    "        '''\n",
    "        conv_op=tf.nn.conv2d(input_data,W,strides=[1,s_h,s_w,1],padding=padding,name=name+'_'+'conv')\n",
    "        \n",
    "        result=tf.add(conv_op,b,name=name+'_'+'add')\n",
    "        \n",
    "        # 默认激活函数是relu\n",
    "        result_act=tf.nn.relu(result,name=name+'_'+'activation')\n",
    "        return result_act"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(TensorShape([Dimension(2), Dimension(3), Dimension(4)]),\n",
       " TensorShape([Dimension(4)]))"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "initial_W = tf.truncated_normal(shape=[2,3,4],stddev=0.1)\n",
    "W=tf.Variable(initial_W,name='weights')\n",
    "\n",
    "initial_b = tf.constant(0.1,shape=[int(W.shape[-1])])\n",
    "b=tf.Variable(initial_b,name='weights')\n",
    "W.shape,b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_input=tf.placeholder(shape=(None,28,28,1),dtype=tf.float32)\n",
    "y_input=tf.placeholder(shape=(None,10),dtype=tf.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor 'conv_i_2/conv_i_add:0' shape=(?, 28, 28, 64) dtype=float32>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conv_2d(x_input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "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.6.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
