{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Getting Started With TensorFlow\n", "https://www.tensorflow.org/get_started/get_started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Check TF installation and version\n", "\n", "## 텐서플로우 설치 및 버전 확인" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "'1.0.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import tensorflow as tf\n", "tf.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hello TensorFlow!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'Hello, TensorFlow!'\n" ] } ], "source": [ "# Create a constant op\n", "# This op is added as a node to the default graph\n", "hello = tf.constant(\"Hello, TensorFlow!\")\n", "\n", "# start a TF session\n", "sess = tf.Session()\n", "\n", "# run the op and get result\n", "print(sess.run(hello))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b’String’ ‘b’ indicates Bytes literals. \n", "\n", "http://stackoverflow.com/questions/6269765/" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Tensors\n", "\n", "### Rank \n", "\n", "https://www.tensorflow.org/programmers_guide/dims_types\n", "\n", "몇 차원인가?\n", "\n", "
\n",
"t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
"\n",
"\n",
"위 예시의 Rank는 2\n",
"\n",
"| Rank | Math entity | Python example |\n",
"| :--: | :---------: | :---------------------------------: |\n",
"| 0 | Scalar | s = 483 |\n",
"| 1 | Vector | v = [1.1,2.2,3.3] |\n",
"| 2 | Matrix | s = [[1,2,3],[4,5,6],[7,8,9]] |\n",
"| 3 | 3-Tensor | t = [[[2],[4],[6]],[[8],[10],[12]]] |\n",
"| n | n-Tensor | ... |\n",
"\n",
"### Shape\n",
"\n",
"각각의 element에 몇개씩 들어있는가?\n",
"\n",
"\n",
"t = [[1,2,3], [4,5,6], [7,8,9]]\n",
"\n",
"\n",
"안쪽의 차원 [바깥쪽, 안쪽] = [3,3]\n",
"\n",
"| Rank | Shape | Dimension number | Example |\n",
"| :--: | :--------------: | :--------------: | :-----------------------------------: |\n",
"| 0 | [] | 0-D | A 0-D tensor. A scalar. |\n",
"| 1 | [D0] | 1-D | A 1-D tensor with shape [5]. |\n",
"| 2 | [D0,D1] | 2-D | A 2-D tensor with shape [3,4]. |\n",
"| 3 | [D0,D1,D2] | 3-D | A 3-D tensor with shape [1,4,3]. |\n",
"| n | [D0,D1,...,Dn-1] | n-D | A tensor with shape [D0,D1,...,Dn-1]. |\n",
"\n",
"### Type\n",
"\n",
"https://www.quora.com/When-should-I-use-tf-float32-vs-tf-float64-in-TensorFlow\n",
"\n",
"| Data type | Python type | Description |\n",
"| :-------: | :------------: | :---------------------: |\n",
"| DT_FLOAT | tf.float32 | 32 bits floating point. |\n",
"| DT_DOUBLE | tf.float64 | 64 bits floating point. |\n",
"| DT_INT8 | tf.int8 | 8 bits signed integer. |\n",
"| DT_INT16 | tf.int16 | 16 bits signed integer. |\n",
"| DT_INT32 | tf.int16 | 32 bits signed integer. |\n",
"| DT_INT64 | tf.int16 | 64 bits signed integer. |"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[[[1.0, 2.0, 3.0]], [[7.0, 8.0, 9.0]]]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 # a rank 0 tensor; this is a scalar with shape []\n",
"[1. ,2., 3.] # a rank 1 tensor; this is a vector with shape [3]\n",
"[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]\n",
"[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Computational Graph"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(1) Build graph (tensors) using TensorFlow operations\n",
"\n",
"(1) 텐서플로 연산을 사용하여 그래프를 빌드합니다."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"node1 = tf.constant(3.0, tf.float32)\n",
"node2 = tf.constant(4.0) # also tf.float32 implicitly\n",
"node3 = tf.add(node1, node2)\n",
"# node3 = node1 + node2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node1: Tensor(\"Const_1:0\", shape=(), dtype=float32) node2: Tensor(\"Const_2:0\", shape=(), dtype=float32)\n",
"node3: Tensor(\"Add:0\", shape=(), dtype=float32)\n"
]
}
],
"source": [
"print(\"node1:\", node1, \"node2:\", node2)\n",
"print(\"node3: \", node3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(2) feed data and run graph (operation)\n",
"\n",
"(2) 데이터를 공급하고 그래프(연산)을 실행합니다.\n",
"\n",
"** sess.run(op) **\n",
"\n",
"(3) update variables in the graph ( and return values)\n",
"\n",
"(3) 그래프에서 이 업데이트 되고 값들을 리턴합니다."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sess.run(node1, node2): [3.0, 4.0]\n",
"sess.run(node3): 7.0\n"
]
}
],
"source": [
"sess = tf.Session()\n",
"print(\"sess.run(node1, node2): \", sess.run([node1, node2]))\n",
"print(\"sess.run(node3): \", sess.run(node3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Placeholder\n",
"\n",
"실행시킬 때 값을 전달하고 싶을때 Placeholder 사용.\n",
"\n",
"sess.run 에서 feed_dict를 통해 값을 전달."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7.5\n",
"[ 3. 7.]\n"
]
}
],
"source": [
"a = tf.placeholder(tf.float32)\n",
"b = tf.placeholder(tf.float32)\n",
"adder_node = a + b # + provides a shortcut for tf.add(a, b)\n",
"\n",
"print(sess.run(adder_node, feed_dict={a: 3, b: 4.5}))\n",
"print(sess.run(adder_node, feed_dict={a: [1,3], b: [2, 4]}))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22.5\n"
]
}
],
"source": [
"add_and_triple = adder_node * 3.\n",
"print(sess.run(add_and_triple, feed_dict={a: 3, b:4.5}))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}