Python으로 시작하는 TensorFlow
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School
출처: Public Domain Vectors
import tensorflow as tf
# 0차원 텐서
d0 = tf.ones((1,))
# 1차원 텐서
d1 = tf.ones((2,))
# 2차원 텐서
d2 = tf.ones((2, 2))
# 3차원 텐서
d3 = tf.ones((2, 2, 2))
# 3차원 텐서 출력
print(d3.numpy())
[[[1. 1.]
[1. 1.]]
[[1. 1.]
[1. 1.]]]
from tensorflow import constant
# 2x3 상수 정의
a = constant(3, shape=[2, 3])
# 2x2 상수 정의
b = constant([1, 2, 3, 4], shape=[2, 2])
| 연산 | 예시 |
|---|---|
tf.constant() |
constant([1, 2, 3]) |
tf.zeros() |
zeros([2, 2]) |
tf.zeros_like() |
zeros_like(input_tensor) |
tf.ones() |
ones([2, 2]) |
tf.ones_like() |
ones_like(input_tensor) |
tf.fill() |
fill([3, 3], 7) |
import tensorflow as tf
# 변수 정의
a0 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.float32)
a1 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.int16)
# 상수 정의
b = tf.constant(2, tf.float32)
# 곱 계산
c0 = tf.multiply(a0, b)
c1 = a0*b
Python으로 시작하는 TensorFlow