The Algorithms logo
The Algorithms
Про AlgorithmsПожертвувати

Basic of Tensor Flow

H
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Basic of TensorFlow.ipynb","version":"0.3.2","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"PEIqWp_Ajco2","colab_type":"text"},"source":["#**TensorFlow**"]},{"cell_type":"code","metadata":{"id":"xiljd-w4r8D5","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":202},"outputId":"a29b5444-b2f4-4826-f6c0-9264da4f00c9","executionInfo":{"status":"ok","timestamp":1568463145418,"user_tz":-330,"elapsed":3395,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n"],"execution_count":27,"outputs":[{"output_type":"stream","text":["--2019-09-14 12:12:29--  https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n","Resolving bin.equinox.io (bin.equinox.io)... 52.204.136.9, 3.214.163.243, 52.54.237.49, ...\n","Connecting to bin.equinox.io (bin.equinox.io)|52.204.136.9|:443... connected.\n","HTTP request sent, awaiting response... 200 OK\n","Length: 13607069 (13M) [application/octet-stream]\n","Saving to: ‘ngrok-stable-linux-amd64.zip’\n","\n","\r          ngrok-sta   0%[                    ]       0  --.-KB/s               \r         ngrok-stab  64%[===========>        ]   8.43M  41.9MB/s               \rngrok-stable-linux- 100%[===================>]  12.98M  40.9MB/s    in 0.3s    \n","\n","2019-09-14 12:12:30 (40.9 MB/s) - ‘ngrok-stable-linux-amd64.zip’ saved [13607069/13607069]\n","\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"4mvo4BebsFsF","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":50},"outputId":"16dc0dd7-5b5e-498c-d899-a5fd29ade248","executionInfo":{"status":"ok","timestamp":1568463166696,"user_tz":-330,"elapsed":2990,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["!unzip ngrok-stable-linux-amd64.zip"],"execution_count":28,"outputs":[{"output_type":"stream","text":["Archive:  ngrok-stable-linux-amd64.zip\n","  inflating: ngrok                   \n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"GX2b0F9csGx9","colab_type":"code","colab":{}},"source":["LOG_DIR = './log'\n","get_ipython().system_raw(\n","'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'\n",".format(LOG_DIR)\n",")"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"6qraPiqesGnF","colab_type":"code","colab":{}},"source":["get_ipython().system_raw('./ngrok http 6006 &')"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"08Nt7vuysGjV","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"c0b5b743-0b21-44b0-ffb4-e95420bc3367","executionInfo":{"status":"ok","timestamp":1568463203880,"user_tz":-330,"elapsed":2778,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["! curl -s http://localhost:4040/api/tunnels | python3 -c \\\n","\"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])\""],"execution_count":31,"outputs":[{"output_type":"stream","text":["https://f4e76656.ngrok.io\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"U1rIp6d6ipiM","colab_type":"code","colab":{}},"source":["import tensorflow as tf"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"xa3Rocvoivc0","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":67},"outputId":"84fbbbe8-0735-49ba-ef44-77eba67eae3b","executionInfo":{"status":"ok","timestamp":1568460837092,"user_tz":-330,"elapsed":909,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["a = tf.constant(10)\n","b = tf.constant(10)\n","c = tf.add(a,b)\n","print(a)\n","print(b)\n","with tf.Session() as sess:\n","  print(sess.run(c))"],"execution_count":6,"outputs":[{"output_type":"stream","text":["Tensor(\"Const_6:0\", shape=(), dtype=int32)\n","Tensor(\"Const_7:0\", shape=(), dtype=int32)\n","20\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"PEA9Vel3jG_G","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":67},"outputId":"0505cf2c-ea75-4b34-80fd-6c00a42123ac","executionInfo":{"status":"ok","timestamp":1568461402364,"user_tz":-330,"elapsed":895,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["\n","a=tf.constant([2,2],name='a')\n","b=tf.constant([[0,1],[2,3]],name='b')\n","x=tf.add(a,b,name='add')\n","y=tf.multiply(a,b,name='mul')\n","with tf.Session() as sess:\n"," x,y=sess.run([x,y])\n","print(x,y)"],"execution_count":7,"outputs":[{"output_type":"stream","text":["[[2 3]\n"," [4 5]] [[0 2]\n"," [4 6]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"hZtuBtLQla08","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":67},"outputId":"d239d562-6f88-4cd0-b44d-2b5facd723af","executionInfo":{"status":"ok","timestamp":1568461653049,"user_tz":-330,"elapsed":903,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["input_tensor = tf.constant([[0,1], [2,3] , [4,5]])\n","like_t = tf.ones_like(input_tensor)\n","my_mul = tf.multiply(like_t , input_tensor , name = \"mul\")\n","\n","with tf.Session() as sess:\n","  my_mul = sess.run(my_mul)\n","  \n","print(my_mul)"],"execution_count":10,"outputs":[{"output_type":"stream","text":["[[0 1]\n"," [2 3]\n"," [4 5]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"xKbFVIlzmPDu","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":168},"outputId":"b396f610-a01e-4cd0-fc4c-a784e680e7e1","executionInfo":{"status":"ok","timestamp":1568462757185,"user_tz":-330,"elapsed":877,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["import numpy as np\n","\n","m1 = [[1 ,2] ,[3 ,4]]\n","\n","m2 = np.array([[1 ,2],[3,4]] , dtype= np.float32)\n","\n","m3 = tf.constant([[1,2] , [3,4]])\n","\n","print(m1)\n","print(m2)\n","print(\"\\n m3 :\" ,m3)\n","\n","t1 = tf.convert_to_tensor(m1 , dtype = tf.float32)\n","t2 = tf.convert_to_tensor(m2 , dtype = tf.float32)\n","t3 = tf.convert_to_tensor(m3 , dtype = tf.int32)\n","\n","print(t1)\n","print(t2)\n","print(t3)\n"],"execution_count":26,"outputs":[{"output_type":"stream","text":["[[1, 2], [3, 4]]\n","[[1. 2.]\n"," [3. 4.]]\n","\n"," m3 :\n"," Tensor(\"Const_42:0\", shape=(2, 2), dtype=int32)\n","Tensor(\"Const_43:0\", shape=(2, 2), dtype=float32)\n","Tensor(\"Const_44:0\", shape=(2, 2), dtype=float32)\n","Tensor(\"Const_42:0\", shape=(2, 2), dtype=int32)\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"lX53MUN2owLu","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]}
Про Алгоритм

#TensorFlow

!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
--2019-09-14 12:12:29--  https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
Resolving bin.equinox.io (bin.equinox.io)... 52.204.136.9, 3.214.163.243, 52.54.237.49, ...
Connecting to bin.equinox.io (bin.equinox.io)|52.204.136.9|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13607069 (13M) [application/octet-stream]
Saving to: ‘ngrok-stable-linux-amd64.zip’


          ngrok-sta   0%[                    ]       0  --.-KB/s               
         ngrok-stab  64%[===========>        ]   8.43M  41.9MB/s               
ngrok-stable-linux- 100%[===================>]  12.98M  40.9MB/s    in 0.3s    

2019-09-14 12:12:30 (40.9 MB/s) - ‘ngrok-stable-linux-amd64.zip’ saved [13607069/13607069]

!unzip ngrok-stable-linux-amd64.zip
Archive:  ngrok-stable-linux-amd64.zip
  inflating: ngrok                   
LOG_DIR = './log'
get_ipython().system_raw(
'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
.format(LOG_DIR)
)
get_ipython().system_raw('./ngrok http 6006 &')
! curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
https://f4e76656.ngrok.io
import tensorflow as tf
a = tf.constant(10)
b = tf.constant(10)
c = tf.add(a,b)
print(a)
print(b)
with tf.Session() as sess:
  print(sess.run(c))
Tensor("Const_6:0", shape=(), dtype=int32)
Tensor("Const_7:0", shape=(), dtype=int32)
20

a=tf.constant([2,2],name='a')
b=tf.constant([[0,1],[2,3]],name='b')
x=tf.add(a,b,name='add')
y=tf.multiply(a,b,name='mul')
with tf.Session() as sess:
 x,y=sess.run([x,y])
print(x,y)
[[2 3]
 [4 5]] [[0 2]
 [4 6]]
input_tensor = tf.constant([[0,1], [2,3] , [4,5]])
like_t = tf.ones_like(input_tensor)
my_mul = tf.multiply(like_t , input_tensor , name = "mul")

with tf.Session() as sess:
  my_mul = sess.run(my_mul)
  
print(my_mul)
[[0 1]
 [2 3]
 [4 5]]
import numpy as np

m1 = [[1 ,2] ,[3 ,4]]

m2 = np.array([[1 ,2],[3,4]] , dtype= np.float32)

m3 = tf.constant([[1,2] , [3,4]])

print(m1)
print(m2)
print("\n m3 :" ,m3)

t1 = tf.convert_to_tensor(m1 , dtype = tf.float32)
t2 = tf.convert_to_tensor(m2 , dtype = tf.float32)
t3 = tf.convert_to_tensor(m3 , dtype = tf.int32)

print(t1)
print(t2)
print(t3)
[[1, 2], [3, 4]]
[[1. 2.]
 [3. 4.]]

 m3 :
 Tensor("Const_42:0", shape=(2, 2), dtype=int32)
Tensor("Const_43:0", shape=(2, 2), dtype=float32)
Tensor("Const_44:0", shape=(2, 2), dtype=float32)
Tensor("Const_42:0", shape=(2, 2), dtype=int32)