codingCat banner
codingCat banner

tensorflow 사용법


 

tensorflow 기본 사용법


본 포스트에서는 텐서플로우의 기본 사용법에 대해 정리한다.

 

텐서 초기화


0으로 채워진 텐서는 zeros(형상)으로 얻는다.

# python Source
import tensorflow as tf

tf.InteractiveSession()

print("scalar")
tensorRank0 = tf.zeros(1) # scalar
print(tensorRank0.eval())

print("vector eg. (x, y)")
tensorRank1 = tf.zeros(2) # vector eg. (x, y)
print(tensorRank1.eval())

print("vector eg. (x, y, z)")
tensorRank1 = tf.zeros(3) # vector eg. (x, y, z)
print(tensorRank1.eval())

print("matrix row: 2, col: 2")
tensorRank2 = tf.zeros((2,2)) # matrix 2x2 (col: 2 / row: 2)
print(tensorRank2.eval())

print("matrix row: 2, col: 3")
tensorRank2 = tf.zeros((2,3)) # matrix 2x3 (col: 3 / row: 2)
print(tensorRank2.eval())
실행 결과

1로 채워진 텐서는 ones(형상)으로 얻는다.

# python Source
import tensorflow as tf

tf.InteractiveSession()

print("scalar")
tensorRank0 = tf.ones(1) # scalar
print(tensorRank0.eval())

print("vector eg. (x, y)")
tensorRank1 = tf.ones(2) # vector eg. (x, y)
print(tensorRank1.eval())

print("vector eg. (x, y, z)")
tensorRank1 = tf.ones(3) # vector eg. (x, y, z)
print(tensorRank1.eval())

print("matrix row: 2, col: 2")
tensorRank2 = tf.ones((2,2)) # matrix 2x2 (col: 2 / row: 2)
print(tensorRank2.eval())

print("matrix row: 2, col: 3")
tensorRank2 = tf.ones((2,3)) # matrix 2x3 (col: 3 / row: 2)
print(tensorRank2.eval())

일반적인 값으로 채워진 텐서는 fill(형상, value=값)으로 얻는다. 단, 랭크 1의 텐서(행렬)만이 가능하다.

# python Source
import tensorflow as tf

tf.InteractiveSession()

print("matrix row: 2, col: 2")
tensorRank2 = tf.ones((2,2)) # matrix 2x2 (col: 2 / row: 2)
print(tensorRank2.eval())

print("matrix row: 2, col: 3")
tensorRank2 = tf.ones((2,3)) # matrix 2x3 (col: 3 / row: 2)
print(tensorRank2.eval())

trackbacks: 0, comments: 0
 
Comments
 
 
'Application Programming Interface/Tensorflow' Related Articles