107 lines
2.2 KiB
Plaintext
107 lines
2.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Tensor(\"Mul_11:0\", shape=(4,), dtype=int32)\n",
|
|
"[ 5 12 21 32]\n",
|
|
"[ 5 12 21 32]\n",
|
|
"30\n",
|
|
"30\n",
|
|
"30\n",
|
|
"30\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Import `tensorflow`\n",
|
|
"import tensorflow as tf\n",
|
|
"import os\n",
|
|
"\n",
|
|
"# Initialize two constants\n",
|
|
"x1 = tf.constant([1,2,3,4])\n",
|
|
"x2 = tf.constant([5,6,7,8])\n",
|
|
"\n",
|
|
"# Multiply\n",
|
|
"result = tf.multiply(x1, x2)\n",
|
|
"\n",
|
|
"# Print the result\n",
|
|
"print(result)\n",
|
|
"\n",
|
|
"# Intialize the Session\n",
|
|
"sess = tf.Session()\n",
|
|
"\n",
|
|
"# Print the result\n",
|
|
"print(sess.run(result))\n",
|
|
"\n",
|
|
"# Close the session\n",
|
|
"sess.close()\n",
|
|
"\n",
|
|
"#Or you can run the session like so:\n",
|
|
"with tf.Session() as sess:\n",
|
|
" output = sess.run(result)\n",
|
|
" print(output)\n",
|
|
"\n",
|
|
" \n",
|
|
"y1=tf.constant(5)\n",
|
|
"y2=tf.constant(6)\n",
|
|
"result=tf.multiply(y1, y2)\n",
|
|
"sess=tf.Session()\n",
|
|
"print(sess.run(result))\n",
|
|
"sess.close()\n",
|
|
"\n",
|
|
"#or\n",
|
|
"\n",
|
|
"with tf.Session() as sess:\n",
|
|
" print (sess.run(result))\n",
|
|
" \n",
|
|
"#this closes the session automatically\n",
|
|
"\n",
|
|
"#try this:\n",
|
|
"with tf.Session() as sess:\n",
|
|
" output=sess.run(result)\n",
|
|
" print (output)\n",
|
|
" \n",
|
|
"print (output)\n",
|
|
"#you can't run sess.run(result) outside of the with action"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"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.6.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|