본문 바로가기

코딩/Machine Learning

TensorFlow Basics(1.X버전)

What is a Data Flow Graph?

Nodes in the graph represent mathematical operations

Edges represent the multidimensional data arrays (tensors) communicated between them.

Installing TensorFlow

Linux, Max OSX, Windows

(sudo -H) pip install --upgrade tensorflow

(sudo -H) pip install --upgrade tensorflow-gpu

From source

bazel ...

https://www.tensorflow.org/install/install_sources

Google search/Community help

https://www.facebook.com/groups/TensorFlowKR/

Check installation and version

Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import tensorflow as tf >>> tf.__version__ '1.0.0' >>>

import tensorflow as tf tf.__version__

Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'1.0.0'
>>>


import tensorflow as tf
tf.__version__

# Create a constant op
# This op is added as a node to the default graph
hello = tf.constant("Hello, TensorFlow!")

# start a TF session
sess = tf.Session()

# run the op and get result
print(sess.run(hello))

b'Hello, TensorFlow!'

 

Tensors

3 # a rank 0 tensor; this is a scalar with shape []
[1. ,2., 3.] # a rank 1 tensor; this is a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

[[[1.0, 2.0, 3.0]], [[7.0, 8.0, 9.0]]]

Computational Graph

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
node3 = tf.add(node1, node2)


print("node1:", node1, "node2:", node2)
print("node3: ", node3)


node1: Tensor("Const_1:0", shape=(), dtype=float32) node2: Tensor("Const_2:0", shape=(), dtype=float32)
node3:  Tensor("Add:0", shape=(), dtype=float32)

sess = tf.Session()
print("sess.run(node1, node2): ", sess.run([node1, node2]))
print("sess.run(node3): ", sess.run(node3))

sess.run(node1, node2):  [3.0, 4.0]
sess.run(node3):  7.0
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b  # + provides a shortcut for tf.add(a, b)

print(sess.run(adder_node, feed_dict={a: 3, b: 4.5}))
print(sess.run(adder_node, feed_dict={a: [1,3], b: [2, 4]}))

7.5
[ 3.  7.]
add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, feed_dict={a: 3, b:4.5}))

22.5

텐서 1차버전 코드들이 있기 때문에 개념 위주로 공부할 것

출처 - https://www.youtube.com/user/hunkims

'코딩 > Machine Learning' 카테고리의 다른 글

How to minimize cost  (0) 2021.01.22
Linear Regression2 and placeholders (1.X버전)  (0) 2021.01.22
Linear Regression  (0) 2021.01.22
Machine Learning Basics  (0) 2021.01.22