[1] 텐서플로우를 위한 자료형

15384 단어 tensorflowtensorflow

상수(constant)

1차원 배열의 연산

import tensorflow as tf
t1 = tf.constant([1,2,3])
t2 = tf.constant([1,2,3])

print(t1+t2) 

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

2차원 배열의 연산

import tensorflow as tf
t1 = tf.constant([[1,2],
                  [2,3]])
t2 = tf.constant([[1,2],
                  [2,3]])

print(t1+t2)

 tf.Tensor(
 [[2 4]
  [4 6]], shape=(2, 2), dtype=int32)

연산 메서드

tf.add()
tf.subtract()
tf.divide()
tf.multiply()
tf.matmul() # 행열의 곱셈연산(단순곱셈과다름)

print(tf.add(t1,t2)) # 예시

0으로 가득찬 배열 만들기

t1 = tf.zeros(10)
print(t1)

tf.Tensor([0. 0. 0. 0. 0. 0. 0. 0. 0. 0.], shape=(10,), dtype=float32)

## 

t1 = tf.zeros([2,3])
print(t1)

tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]], shape=(2, 3), dtype=float32)
 
 ##
 
t1 = tf.zeros([2,3,4])
print(t1)

tf.Tensor(
[[[0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]]

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

shape

t1 = tf.zeros([2,3,4])
print(t1.shape)

# (2, 3, 4)

텐서 자료형이 몇차원 배열인지 알려줌

dtype

t2 = tf.constant("노")
print(t2)

#tf.Tensor(b'\xeb\x85\xb8\xe3\x85\x91', shape=(), dtype=string)

dtype으로 데이터 타입정보를 가지고있음
ex) int, float, string...

변수(Variable)

w = tf.Variable(1.0) # 텐서플로우에서 weight(가중치)로 사용됨
print(w)
#<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.0>

print(t.numpy()) # 값을 참조할 때
# 1.0

t.assign(3) # 변수니까 새로운값 할당 가능
print(t.numpy())
# 3

좋은 웹페이지 즐겨찾기