MJay

TensorFLow Basic Syntax 본문

카테고리 없음

TensorFLow Basic Syntax

MJSon 2019. 3. 21. 22:06


import tensorflow as tf
In [3]:
print (tf.__version__)
1.13.1
In [12]:
hello = tf.constant("Hello ")
In [13]:
world = tf.constant("World")
In [14]:
type(hello)
Out[14]:
tensorflow.python.framework.ops.Tensor
In [15]:
print(hello)
Tensor("Const_2:0", shape=(), dtype=string)
In [16]:
with tf.Session() as sess:
    result = sess.run(hello + world)
In [17]:
print(result)
b'Hello World'
In [18]:
a = tf.constant(10)
In [21]:
b = tf.constant(20)
In [22]:
a + b
Out[22]:
<tf.Tensor 'add_3:0' shape=() dtype=int32>
In [23]:
a + b
Out[23]:
<tf.Tensor 'add_4:0' shape=() dtype=int32>
In [24]:
a + b
Out[24]:
<tf.Tensor 'add_5:0' shape=() dtype=int32>
In [25]:
type(a)
Out[25]:
tensorflow.python.framework.ops.Tensor
In [26]:
with tf.Session() as sess:
    result = sess.run(a+b)
In [27]:
result
Out[27]:
30
In [28]:
const = tf.constant(10)
In [31]:
fill_mat = tf.fill((4,4),10)
In [32]:
myzeros = tf.zeros((4,4))
In [33]:
myones = tf.ones((4,4))
In [34]:
myrandn = tf.random_normal((4,4), mean=0, stddev=1.0)
In [35]:
myrandu = tf.random_uniform((4,4),minval=0, maxval =1)
In [36]:
my_ops = [const,fill_mat,myzeros,myones, myrandn, myrandu]
In [37]:
sess = tf.InteractiveSession()
In [39]:
for op in my_ops:
    print(op.eval()) # or you can do sess.run(op)
    print("\n")
10


[[10 10 10 10]
 [10 10 10 10]
 [10 10 10 10]
 [10 10 10 10]]


[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]


[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]


[[ 0.490945   -0.38291478  0.16350803  0.11003197]
 [ 1.2281536   2.8937936   0.21065642 -2.0964367 ]
 [ 1.1583276   0.20911726  1.2566063   0.9805608 ]
 [ 0.89962894  0.57698524  0.18294382 -2.1676404 ]]


[[0.18413973 0.05404115 0.78048813 0.02524388]
 [0.9484662  0.8964958  0.680035   0.90450466]
 [0.82825315 0.38664615 0.6516963  0.00248575]
 [0.04025424 0.5726043  0.16049802 0.08199143]]


Matrix Multiplication

In [41]:
a = tf.constant([[1,2],[3,4]])
In [42]:
a.get_shape()
Out[42]:
TensorShape([Dimension(2), Dimension(2)])
In [43]:
b = tf.constant([[10],[20]])
In [44]:
b.get_shape()
Out[44]:
TensorShape([Dimension(2), Dimension(1)])
In [45]:
result = tf.matmul(a,b)
In [46]:
sess.run(result)
Out[46]:
array([[ 50],
       [110]], dtype=int32)
In [47]:
result.eval() # or you can do sess.run(result)
Out[47]:
array([[ 50],
       [110]], dtype=int32)

What is interactive session

A TensorFlow Session for use in interactive contexts, such as a shell.

The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods tf.Tensor.eval and tf.Operation.run will use that session to run ops.

This is convenient in interactive shells and IPython notebooks, as it avoids having to pass an explicit Session object to run ops.

What is session

TensorFlow uses the tf.Session class to represent a connection between the client program---typically a Python program, although a similar interface is available in other languages---and the C++ runtime. A tf.Session object provides access to devices in the local machine, and remote devices using the distributed TensorFlow runtime. It also caches information about your tf.Graph so that you can efficiently run the same computation multiple times.

In [ ]: