import tensorflow as tf
tf.__version__
'1.0.1'
# 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!'
b’String’ ‘b’ indicates Bytes literals.
https://www.tensorflow.org/programmers_guide/dims_types
몇 차원인가?
t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
위 예시의 Rank는 2
Rank | Math entity | Python example |
---|---|---|
0 | Scalar | s = 483 |
1 | Vector | v = [1.1,2.2,3.3] |
2 | Matrix | s = [[1,2,3],[4,5,6],[7,8,9]] |
3 | 3-Tensor | t = [[[2],[4],[6]],[[8],[10],[12]]] |
n | n-Tensor | ... |
각각의 element에 몇개씩 들어있는가?
t = [[1,2,3], [4,5,6], [7,8,9]]
안쪽의 차원 [바깥쪽, 안쪽] = [3,3]
Rank | Shape | Dimension number | Example |
---|---|---|---|
0 | [] | 0-D | A 0-D tensor. A scalar. |
1 | [D0] | 1-D | A 1-D tensor with shape [5]. |
2 | [D0,D1] | 2-D | A 2-D tensor with shape [3,4]. |
3 | [D0,D1,D2] | 3-D | A 3-D tensor with shape [1,4,3]. |
n | [D0,D1,...,Dn-1] | n-D | A tensor with shape [D0,D1,...,Dn-1]. |
https://www.quora.com/When-should-I-use-tf-float32-vs-tf-float64-in-TensorFlow
Data type | Python type | Description |
---|---|---|
DT_FLOAT | tf.float32 | 32 bits floating point. |
DT_DOUBLE | tf.float64 | 64 bits floating point. |
DT_INT8 | tf.int8 | 8 bits signed integer. |
DT_INT16 | tf.int16 | 16 bits signed integer. |
DT_INT32 | tf.int16 | 32 bits signed integer. |
DT_INT64 | tf.int16 | 64 bits signed integer. |
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]]]
(1) Build graph (tensors) using TensorFlow operations
(1) 텐서플로 연산을 사용하여 그래프를 빌드합니다.
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
node3 = tf.add(node1, node2)
# node3 = 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)
(2) feed data and run graph (operation)
(2) 데이터를 공급하고 그래프(연산)을 실행합니다.
** sess.run(op) **
(3) update variables in the graph ( and return values)
(3) 그래프에서 이 업데이트 되고 값들을 리턴합니다.
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