{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "warnings.simplefilter(action='ignore')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. 创建计算图" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1.1 创建常数" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "c = tf.constant(2, name='c')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1.2 创建变量" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "var = tf.Variable(c + 5, name='var')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. 执行计算图" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "sess = tf.Session()\n", "init = tf.global_variables_initializer()\n", "sess.run(init)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c: 2\n", "var: 7\n" ] } ], "source": [ "print('c:', sess.run(c))\n", "print('var:', sess.run(var))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c: 2\n", "var: 7\n" ] } ], "source": [ "print('c:', c.eval(session=sess))\n", "print('var:', var.eval(session=sess))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "sess.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. 使用 with 语句打开 Session" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c: 2\n", "var: 7\n" ] } ], "source": [ "c = tf.constant(2, name='c')\n", "var = tf.Variable(c + 5, name='var')\n", "with tf.Session() as sess:\n", " init = tf.global_variables_initializer()\n", " sess.run(init)\n", " print('c:', sess.run(c))\n", " print('var:', sess.run(var))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. placeholder" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "width = tf.placeholder('int32')\n", "height = tf.placeholder('int32')\n", "area = tf.multiply(width, height)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "area: 48\n" ] } ], "source": [ "with tf.Session() as sess:\n", " init = tf.global_variables_initializer()\n", " sess.run(init)\n", " print('area:', sess.run(area, feed_dict={width: 6, height: 8}))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. 创建多维 tensor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5.1 创建一维 tensor" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.4 0.2 0.4]\n" ] } ], "source": [ "var = tf.Variable([0.4, 0.2, 0.4])\n", "with tf.Session() as sess:\n", " sess.run(tf.global_variables_initializer())\n", " result = sess.run(var)\n", " print(result)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3,)\n" ] } ], "source": [ "print(result.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5.2 创建二维 tensor" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.4 0.2 0.4]]\n" ] } ], "source": [ "var = tf.Variable([[0.4, 0.2, 0.4]])\n", "with tf.Session() as sess:\n", " sess.run(tf.global_variables_initializer())\n", " result = sess.run(var)\n", " print(result)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 3)\n" ] } ], "source": [ "print(result.shape)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.5 -0.2]\n", " [-0.3 0.4]\n", " [-0.5 0.2]]\n" ] } ], "source": [ "W = tf.Variable([\n", " [-0.5, -0.2],\n", " [-0.3, 0.4],\n", " [-0.5, 0.2]\n", "])\n", "with tf.Session() as sess:\n", " sess.run(tf.global_variables_initializer())\n", " result = sess.run(W)\n", " print(result)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3, 2)\n" ] } ], "source": [ "print(result.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6. 矩阵基本运算" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.1 矩阵乘法" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "product: [[-1.3 0.4]]\n" ] } ], "source": [ "X = tf.Variable([[1.0, 1.0, 1.0]])\n", "W = tf.Variable([\n", " [-0.5, -0.2],\n", " [-0.3, 0.4],\n", " [-0.5, 0.2]\n", "])\n", "product = tf.matmul(X, W)\n", "\n", "with tf.Session() as sess:\n", " sess.run(tf.global_variables_initializer())\n", " print(\"product:\", sess.run(product))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.2 矩阵加法" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "result: [[-1.1999999 0.6 ]]\n" ] } ], "source": [ "b = tf.Variable([[0.1, 0.2]])\n", "product = tf.Variable([[-1.3, 0.4]])\n", "result = product + b\n", "\n", "with tf.Session() as sess:\n", " sess.run(tf.global_variables_initializer())\n", " print('result:', sess.run(result))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.3 矩阵乘法与加法" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "result: [[-1.1999999 0.6 ]]\n" ] } ], "source": [ "X = tf.Variable([[1.0, 1.0, 1.0]])\n", "W = tf.Variable([\n", " [-0.5, -0.2],\n", " [-0.3, 0.4],\n", " [-0.5, 0.2]\n", "])\n", "b = tf.Variable([[0.1, 0.2]])\n", "result = tf.matmul(X, W) + b\n", "\n", "with tf.Session() as sess:\n", " sess.run(tf.global_variables_initializer())\n", " print('result:', sess.run(result))" ] } ], "metadata": { "kernelspec": { "display_name": "tensorflow-keras-practice", "language": "python", "name": "tensorflow-keras-practice" }, "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }