9566 2022. 3. 28. 21:19
728x90

- tf.constant()
- tf.Variable()
- tf.placeholder()

# tf.constant() : 변하지 않는 상수 생성
# tf.Variable() : 값이 바뀔 수도 있는 변수 생성
import tensorflow as tf
sess = tf.Session()

x1 = tf.constant([10], dtype=tf.float32, name='test1')
x2 = tf.Variable([8], dtype=tf.float32, name='test2')
init = tf.global_variables_initializer()

sess.run(init) # 초기화 먼저 진행
print(sess.run(x1)) # 값 부여


## tf.placeholder + feed_dict : 실행시 feed_dict={x="들어갈 값"}로 값을 지정하여 실행
import tensorflow as tf
input_data = [7,8]
x = tf.placeholder(dtype=tf.float32)
y = x / 2

sess = tf.Session()
print(sess.run(y, feed_dict={x:input_data}))

 

 

- tf.compat.v1.to_int32()

- tf.compat.v1.metrics.mean_iou()

- tf.control_dependencies()

# Define IoU metric
from keras import backend as K

def mean_iou(y_true, y_pred):
    prec = []
    for t in np.arange(0.5, 1.0, 0.05):
        y_pred_ = tf.compat.v1.to_int32(y_pred > t)
        score, up_opt = tf.compat.v1.metrics.mean_iou(y_true, y_pred_, 2)
        K.get_session().run(tf.compat.v1.local_variables_initializer())
        with tf.control_dependencies([up_opt]):
            score = tf.compat.v1.identity(score)
        prec.append(score)
    return K.mean(K.stack(prec), axis=0)

 

 

- tf.cast() : 소수점을 버리고 정수형으로 만듦

a = [1.9999, 2.1111]
tf.cast(a) # [1, 2]

- tf.squeeze() : 1을 제거

a = [1,2,3,4]
tf.squeeze(a) # [2,3,4]

- tf.reduce_mean()

import tensorflow as tf
x = tf.constant([1., 3.] , [2., 4.])
sess = tf.Session()

print(sess.run(tf.reduce_mean(x))) # 2.5
print(sess.run(tf.reduce_mean(x, 0))) # 열, [1.5, 3.5]
print(sess.run(tf.reduce_mean(x, 1))) # 행, [2, 3]

 

- append() vs extend()

a = ['a1', 'b1']
b = ['a2', 'b2']
a.append(b) # ['a1', 'b1', ['a2', 'b2']]
a.extend(b) # ['a1', 'b1', 'a2', 'b2']

728x90