{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.6.0'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "tf.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## first try"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_data():\n",
    "    x_data=np.float32(np.random.rand(2,100))\n",
    "    y_data=np.dot([0.100,0.200],x_data)+0.300\n",
    "    return x_data,y_data\n",
    "\n",
    "x_data,y_data=get_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过tensorflow搭建起vgg\n",
    "\n",
    "b=tf.Variable(tf.zeros([1]))\n",
    "W=tf.Variable(tf.random_uniform([1,2],-1.0,1.0))\n",
    "\n",
    "y=tf.matmul(W,x_data)+b\n",
    "# 定义loss\n",
    "\n",
    "# 这里应该是为了计算batch的时候，算一个batch的均值\n",
    "# 所以采用reduce-mean的吧\n",
    "\n",
    "loss=tf.reduce_mean(tf.square(y-y_data))\n",
    "optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.5)\n",
    "train = optimizer.minimize(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化所有变量\n",
    "# 这里不支持自定义初始化方式\n",
    "init=tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [[0.5204204 0.5038343]] [-0.14161567]\n",
      "20 [[0.22159123 0.3312029 ]] [0.1733526]\n",
      "40 [[0.13583937 0.24765971]] [0.25813115]\n",
      "60 [[0.11116112 0.21665157]] [0.2860473]\n",
      "80 [[0.10359608 0.20570986]] [0.29533035]\n",
      "100 [[0.10118156 0.20193957]] [0.29843363]\n",
      "120 [[0.10039245 0.20065567]] [0.29947397]\n",
      "140 [[0.1001311  0.20022109]] [0.29982322]\n",
      "160 [[0.10004394 0.20007446]] [0.2999406]\n",
      "180 [[0.10001474 0.20002505]] [0.29998]\n",
      "200 [[0.10000497 0.20000844]] [0.29999328]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    # 训练步骤\n",
    "    for step in range(0,201):\n",
    "        sess.run(train)\n",
    "        if step%20 ==0:\n",
    "            print(step,sess.run(W),sess.run(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy try 1:\n",
    "\n",
    "    matrix multiply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# define matirx\n",
    "\n",
    "matrix1=tf.constant([[1,2],[3,4]])\n",
    "matrix2=tf.constant([[5,6],[7,8]])\n",
    "\n",
    "# 默认是2个矩阵的乘法，所以直接乘就可以了\n",
    "product=tf.matmul(matrix1,matrix2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[19 22]\n",
      " [43 50]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    # 所以其实梯度下降的时候，这里返回的是loss值吧？\n",
    "    result=sess.run(product)\n",
    "    print(result)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy try 2:\n",
    "\n",
    "accumulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "定义图里面 op的计算\n",
    "'''\n",
    "\n",
    "# 获得一个累加器\n",
    "conter=tf.Variable(0,name='counter')\n",
    "one=tf.constant(1)\n",
    "\n",
    "'''\n",
    "这里看起来比较麻烦可以认为是tf里面没有+=这个操作，所有对象的拷贝都是深拷贝\n",
    "'''\n",
    "# 累加\n",
    "one_more=tf.add(conter,one)\n",
    "# 赋值 \n",
    "final_step=tf.assign(conter,one_more)\n",
    "\n",
    "# 变量初始化\n",
    "init=tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "sess的执行逻辑是，只需要让她执行最后一步的op就行了。\n",
    "它会根据op之间的依赖关系，一直回溯到最开始的constant或者variable，\n",
    "来开始计算\n",
    "'''\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for _ in range(3):\n",
    "        print(sess.run(final_step))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy try 3:\n",
    "\n",
    "multi fetch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[19, 22],\n",
      "       [43, 50]]), 1]\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "可以同时执行多个op\n",
    "但是initial貌似必须显示的优先执行\n",
    "'''\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    print(sess.run([product,final_step]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy try 4：\n",
    "\n",
    "feed-placeholder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 根据shape来进行设定输入的tensor的shape，dtype来设置参数类型\n",
    "input1=tf.placeholder(shape=(1,2),dtype=tf.float32)\n",
    "input2=tf.placeholder(shape=(2,1),dtype=tf.float32)\n",
    "output=tf.matmul(input1,input2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "test1=np.asarray([[1,2]])\n",
    "test2=np.asarray([[2],[1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    result=sess.run(output,feed_dict={input1:test1,input2:test2})\n",
    "    print(result)"
   ]
  },
  {
   "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
}
