Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save devops-school/78e5d19c9d80d6818318c5fe13ca45a2 to your computer and use it in GitHub Desktop.
Save devops-school/78e5d19c9d80d6818318c5fe13ca45a2 to your computer and use it in GitHub Desktop.
PyTorch Lab - 10 - Pytorch CUDA Semantics
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- w is a tensorflow variable\n",
"- x is a placeholder\n",
"- b is a constant"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"W = tf.Variable(6, name = 'var_W')\n",
"\n",
"x = tf.placeholder(tf.int32, shape = [3] ,name = 'x')\n",
"\n",
"b = tf.constant(3, name = 'constant_b')"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<tf.Variable 'var_W_3:0' shape=() dtype=int32_ref>"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"W"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<tf.Tensor 'x_3:0' shape=(3,) dtype=int32>"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<tf.Tensor 'constant_b_3:0' shape=() dtype=int32>"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"y = W*x + b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### We can't see any values that these variables hold because the graph hasnt been executed yet"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<tf.Tensor 'add_3:0' shape=(3,) dtype=int32>"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Initialize global variables"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"init = tf.global_variables_initializer()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### A Session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated."
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wx+b = [ 63 123 183]\n"
]
}
],
"source": [
"with tf.Session( )as sess:\n",
" sess.run(init)\n",
" \n",
" y_result = sess.run(y, feed_dict = {x:[10, 20, 30]})\n",
" \n",
" print(\"Wx+b = \", y_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The FileWriter class provides a mechanism to create an event file in a given directory and add summaries and events to it\n",
"graphs directory is created in the current working directory "
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"writer = tf.summary.FileWriter('./graphs', sess.graph)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To go to the tensorboard run the following command in your Terminal\n",
"\n",
"#### tensorboard --logdir=graphs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### its a good practice to close the session and writer"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"writer.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Returns the default graph for the current thread."
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"graph = tf.get_default_graph()"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[<tf.Operation 'var_W/initial_value' type=Const>, <tf.Operation 'var_W' type=VariableV2>, <tf.Operation 'var_W/Assign' type=Assign>, <tf.Operation 'var_W/read' type=Identity>, <tf.Operation 'x' type=Placeholder>, <tf.Operation 'constant_b' type=Const>, <tf.Operation 'mul' type=Mul>, <tf.Operation 'add' type=Add>, <tf.Operation 'init' type=NoOp>, <tf.Operation 'var_W_1/initial_value' type=Const>, <tf.Operation 'var_W_1' type=VariableV2>, <tf.Operation 'var_W_1/Assign' type=Assign>, <tf.Operation 'var_W_1/read' type=Identity>, <tf.Operation 'x_1' type=Placeholder>, <tf.Operation 'constant_b_1' type=Const>, <tf.Operation 'mul_1' type=Mul>, <tf.Operation 'add_1' type=Add>, <tf.Operation 'init_1' type=NoOp>, <tf.Operation 'var_W_2/initial_value' type=Const>, <tf.Operation 'var_W_2' type=VariableV2>, <tf.Operation 'var_W_2/Assign' type=Assign>, <tf.Operation 'var_W_2/read' type=Identity>, <tf.Operation 'x_2' type=Placeholder>, <tf.Operation 'constant_b_2' type=Const>, <tf.Operation 'mul_2' type=Mul>, <tf.Operation 'add_2' type=Add>, <tf.Operation 'init_2' type=NoOp>, <tf.Operation 'var_W_3/initial_value' type=Const>, <tf.Operation 'var_W_3' type=VariableV2>, <tf.Operation 'var_W_3/Assign' type=Assign>, <tf.Operation 'var_W_3/read' type=Identity>, <tf.Operation 'x_3' type=Placeholder>, <tf.Operation 'constant_b_3' type=Const>, <tf.Operation 'mul_3' type=Mul>, <tf.Operation 'add_3' type=Add>, <tf.Operation 'init_3' type=NoOp>]\n"
]
}
],
"source": [
"print(graph.get_operations())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment