December 29, 2018
해당 게시물은 Edwith에서 제공하는
머신러닝과 딥러닝 BASIC을 듣고 요약 정리한 글입니다.
Linux, Max OSX, Windows
import tensorflow as tf
tf.__version__
'1.12.0'
'b'
는 바이트 문자를 나타낸다.# Create a constant op
# This op is added as a node to the default graph
hello = tf.constant("Hello, TensorFlow!")
# Seart a TF session
sess = tf.Session()
# Run the op and get result
print(sess.run(hello))
b'Hello, TensorFlow!'
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # 명시적으로 tf.float32형
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)
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
sess.run(op)
를 사용해 그래프 실행node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)
node3 = tf.add(node1, node2)
sess.run(op)
를 사용해 그래프 실행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
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.]
# a rank 0 tensor
# This is a scalar with shape []
3
# a rank 1 tensor
# This is a vector with shape [3]
[1., 2., 3.]
# a rank 2 tensor
# This is a matrix with shape [2, 3]
[[1., 2., 3.], [4., 5., 6.]]
# a rank 3 tensor with shape [2, 1, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]]
[[[1.0, 2.0, 3.0]], [[7.0, 8.0, 9.0]]]
Rank | Math entity | Python example |
---|---|---|
0 | Scalar (magnitude only) | s = 483 |
1 | Vector (magnitude and direction) | v = [1.1, 2.2, 3.3] |
2 | Matrix (table of numbers) | m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] |
3 | 3-Tensor (cube of numbers | t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]] |
n | n-Tensor | … |
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]. |
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.int32 | 32 bits signed integer. |
DT_INT64 | tf.int64 | 64 bits signed integer. |