Skip to content

Commit

Permalink
add examples from tensorflow book.
Browse files Browse the repository at this point in the history
  • Loading branch information
perhapszzy committed Feb 11, 2017
1 parent 270ea5d commit 835b95d
Show file tree
Hide file tree
Showing 3,803 changed files with 120,164 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1. 定义两个不同的图"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0.]\n",
"[ 1.]\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"\n",
"g1 = tf.Graph()\n",
"with g1.as_default():\n",
" v = tf.get_variable(\"v\", [1], initializer = tf.zeros_initializer) # 设置初始值为0\n",
"\n",
"g2 = tf.Graph()\n",
"with g2.as_default():\n",
" v = tf.get_variable(\"v\", [1], initializer = tf.ones_initializer()) # 设置初始值为1\n",
" \n",
"with tf.Session(graph = g1) as sess:\n",
" tf.global_variables_initializer().run()\n",
" with tf.variable_scope(\"\", reuse=True):\n",
" print(sess.run(tf.get_variable(\"v\")))\n",
"\n",
"with tf.Session(graph = g2) as sess:\n",
" tf.global_variables_initializer().run()\n",
" with tf.variable_scope(\"\", reuse=True):\n",
" print(sess.run(tf.get_variable(\"v\")))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"#### 2. 张量的概念"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tensor(\"add:0\", shape=(2,), dtype=float32)\n",
"[ 3. 5.]\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"a = tf.constant([1.0, 2.0], name=\"a\")\n",
"b = tf.constant([2.0, 3.0], name=\"b\")\n",
"result = a + b\n",
"print result\n",
"\n",
"sess = tf.InteractiveSession ()\n",
"print(result.eval())\n",
"sess.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. 会话的使用"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.1 创建和关闭会话"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 3. 5.]\n"
]
}
],
"source": [
"# 创建一个会话。\n",
"sess = tf.Session()\n",
"\n",
"# 使用会话得到之前计算的结果。\n",
"print(sess.run(result))\n",
"\n",
"# 关闭会话使得本次运行中使用到的资源可以被释放。\n",
"sess.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.2 使用with statement 来创建会话"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 3. 5.]\n"
]
}
],
"source": [
"with tf.Session() as sess:\n",
" print(sess.run(result))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.3 指定默认会话"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 3. 5.]\n"
]
}
],
"source": [
"sess = tf.Session()\n",
"with sess.as_default():\n",
" print(result.eval())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 3. 5.]\n",
"[ 3. 5.]\n"
]
}
],
"source": [
"sess = tf.Session()\n",
"\n",
"# 下面的两个命令有相同的功能。\n",
"print(sess.run(result))\n",
"print(result.eval(session=sess))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. 使用tf.InteractiveSession构建会话"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 3. 5.]\n"
]
}
],
"source": [
"sess = tf.InteractiveSession ()\n",
"print(result.eval())\n",
"sess.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 5. 通过ConfigProto配置会话"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)\n",
"sess1 = tf.InteractiveSession(config=config)\n",
"sess2 = tf.Session(config=config)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading

0 comments on commit 835b95d

Please sign in to comment.