Skip to content

Instantly share code, notes, and snippets.

@eblancoh
Created April 23, 2018 07:47
Show Gist options
  • Save eblancoh/afa733a98c667648acf43538523f8cee to your computer and use it in GitHub Desktop.
Save eblancoh/afa733a98c667648acf43538523f8cee to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TensorFlow Graphs"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/eblancoh/anaconda3/envs/universe/lib/python3.5/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
" from ._conv import register_converters as _register_converters\n"
]
}
],
"source": [
"# Importamos la librería TensorFlow\n",
"import tensorflow as tf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generación de un grafo sencillo"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Creamos un par de nodos definidos como dos constantes:\n",
"num_1 = tf.constant(1)\n",
"num_2 = tf.constant(2)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Se define un tercer nodo de operación donde se suman las dos constantes anteriores.\n",
"num_3 = num_1 + num_2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"# Como ya sabemos, abrimos sesión para poder evaluar el resultado de nuestra operación:\n",
"with tf.Session() as sess:\n",
" resultado = sess.run(num_3)\n",
"print(resultado)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tensor(\"add:0\", shape=(), dtype=int32)\n"
]
}
],
"source": [
"print (num_3)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<tensorflow.python.framework.ops.Graph object at 0x7f07b61e7cc0>\n"
]
}
],
"source": [
"# Si ejecutamos el siguiente comando, se nos devolverá el objeto graph por defecto que tenemos ahora \n",
"# mismo almacenado en memoria\n",
"print(tf.get_default_graph())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[<tf.Operation 'Const' type=Const>, <tf.Operation 'Const_1' type=Const>, <tf.Operation 'add' type=Add>]\n"
]
}
],
"source": [
"# TensorFlow también nos permite visualizar todas las operaciones soportadas en el grafo por defecto.\n",
"g = tf.get_default_graph()\n",
"print(g.get_operations())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"El grafo está definido por tres nodos de operaciones: dos de constantes (num_1 y num_2) y uno de suma (num_3)."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Podemos definir otro grafo mediante la siguiente expresión.\n",
"graph = tf.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<tensorflow.python.framework.ops.Graph object at 0x7f07b61e0c50>\n"
]
}
],
"source": [
"# Que es claramente distinto al otro grafo que tenemos definido por defecto en tf.get_default_graph()\n",
"print(graph)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generemos un grafo y convirtámoslo en el que se debe considerar por defecto:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"graph_one = tf.get_default_graph()\n",
"graph_two = tf.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Podemos ver que graph_one es el grafo por defecto\n",
"graph_one is tf.get_default_graph()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"graph_two is tf.get_default_graph()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"with graph_two.as_default():\n",
" print(graph_two is tf.get_default_graph())"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"graph_two is tf.get_default_graph()"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment