반응형

텐서플로우2.0 기초 문법  

 

글. 수알치 오상문

 

"""텐서플로우와 넘파이 임포트"""

import tensorflow as tf
import numpy as np

"""텐서플로우 텐서플로우 버전 확인"""
print(tf.__version__)

 

[결과]

2.4.1


"""텐서 변수, 스칼라"""
s = tf.Variable("hello", tf.string)
n = tf.Variable(100, tf.int32)
f = tf.Variable(10.123, tf.float64)

print(s)
print(n)
print(f)

 

[결과]

<tf.Variable 'Variable:0' shape=() dtype=string, numpy=b'hello'>

<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=100>

<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=10.123>


"""텐서 배열 변수, 벡터와 텐서"""
s = tf.Variable(["hello", "hi"], tf.string)
n = tf.Variable([100, 200, 300], tf.int32)
f = tf.Variable([[10.1, 10.2, 10.3], [20.1, 20.2, 20.3]], tf.float64)


print(s)
print(n)
print(f)

 

[결과] 

<tf.Variable 'Variable:0' shape=(2,) dtype=string, numpy=array([b'hello', b'hi'], dtype=object)>

<tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([100, 200, 300], dtype=int32)>

<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=

array([[10.1, 10.2, 10.3],

        [20.1, 20.2, 20.3]], dtype=float32)>

 

"""텐서 초기화 방법"""
t0 = tf.zeros([2,3])
t1 = tf.ones([2,3])
t2 = tf.eye(3)


print(t0)
print(t1)
print(t2)

 

[결과]

tf.Tensor(

[[0. 0. 0.]

 [0. 0. 0.]], shape=(2, 3), dtype=float32)

tf.Tensor(

[[1. 1. 1.]

 [1. 1. 1.]], shape=(2, 3), dtype=float32)

tf.Tensor(

[[1. 0. 0.]

 [0. 1. 0.]

 [0. 0. 1.]], shape=(3, 3), dtype=float32)


"""텐서 합, 평균, 최대, 최소 reduce 기능"""
data1 = tf.constant([1,2,3,4,5,6,7,8,9,10])


print(tf.reduce_sum(data1))
print(tf.reduce_mean(data1))
print(tf.reduce_max(data1))
print(tf.reduce_min(data1))


data2 = data1 + 5
print(data2)

 

[결과]

tf.Tensor(55, shape=(), dtype=int32)

tf.Tensor(5, shape=(), dtype=int32)

tf.Tensor(10, shape=(), dtype=int32)

tf.Tensor(1, shape=(), dtype=int32)

tf.Tensor([ 6 7 8 9 10 11 12 13 14 15], shape=(10,), dtype=int32)

"""텐서 행렬 연산"""
mat1 = tf.Variable([[1,2,3], [1,2,3], [1,2,3]], tf.int32)
mat2 = tf.Variable([[1,2,3], [1,2,3], [1,2,3]], tf.int32)


print(tf.add(mat1, mat2))       # print(mat1 + mat2) 
print(tf.subtract(mat1, mat2))  # print(mat1 - mat2)
print(mat1 // mat2)
print(tf.matmul(mat1, mat2))    # 행렬 곱셈
print(mat1 * mat2)              # 이건 행렬 곱셈이 아니고 각 원소별 곱이다.

 

[결과] 

tf.Tensor(

[[2 4 6]

 [2 4 6]

 [2 4 6]], shape=(3, 3), dtype=int32)

tf.Tensor(

[[0 0 0]

 [0 0 0]

 [0 0 0]], shape=(3, 3), dtype=int32)

tf.Tensor(

[[0 0 0]

 [0 0 0]

 [0 0 0]], shape=(3, 3), dtype=int32)

tf.Tensor(

[[1 1 1]

 [1 1 1]

 [1 1 1]], shape=(3, 3), dtype=int32)

tf.Tensor(

[[ 6 12 18]

 [ 6 12 18]

 [ 6 12 18]], shape=(3, 3), dtype=int32)


"""텐서 리쉐이프와 값 형변환"""
mat1 = tf.Variable([[1,2,3], [4,5,6]], tf.int32) 
mat2 = tf.reshape(mat1, [3,2])
print(mat2)


mat3 = tf.cast(mat1, tf.float64)
print(mat3)


[결과]

tf.Tensor(

[[1 2]

 [3 4]

 [5 6]], shape=(3, 2), dtype=int32)

tf.Tensor(

[[1. 2. 3.] [4. 5. 6.]], shape=(2, 3), dtype=float64)


"""텐서 슬라이싱"""
mat1 = tf.Variable([[1,2,3], [4,5,6]], tf.int32) 
mat2 = tf.slice(mat1, [0,1], [2,2])  # [시작 위치], [사이즈]


print(mat1)
print(mat2)

 

[결과]

<tf.Variable 'Variable:0' shape=(2, 3) dtype=int32, numpy=

array([[1, 2, 3],

        [4, 5, 6]], dtype=int32)>

tf.Tensor(

[[2 3]

 [5 6]], shape=(2, 2), dtype=int32)


"""텐서 합치기 concat, stack"""
a = tf.Variable([[1,2,3], [4,5,6]], tf.int32)
b = tf.Variable([[7,8,9], [10,11,12]], tf.int32)
c = tf.concat([a, b], 0) # 아래로(Y축) 연결
print(c)


c = tf.concat([a, b], 1) # 옆으로(X축) 연결
print(c)


c = tf.stack([a,b], 0)  # a, b를 아래로 묶어서 쌓은 자료
print(c)


c = tf.stack([a,b], 1)  # a, b 열 부분을 묶어서 아래로 쌓은 자료 
print(c)


[결과]

tf.Tensor(

[[ 1 2 3]

 [ 4 5 6]

 [ 7 8 9]

 [10 11 12]], shape=(4, 3), dtype=int32)

tf.Tensor(

[[ 1 2 3 7 8 9]

 [ 4 5 6 10 11 12]], shape=(2, 6), dtype=int32)

tf.Tensor(

[[[ 1 2 3]

  [ 4 5 6]]

 

 [[ 7 8 9]

  [10 11 12]]], shape=(2, 2, 3), dtype=int32)

tf.Tensor(

[[[ 1 2 3]

  [ 7 8 9]]

 

 [[ 4 5 6]

  [10 11 12]]], shape=(2, 2, 3), dtype=int32)

 

<이상>

반응형

+ Recent posts