tf.constant, tf.Variable, tf.placeholder
텐서플로우의 기본이 되는 함수
tf.constant(value, dtype=None, shape=None,name=’Const’, verify_shape=False)
상수 텐서를 만드는 함수. value 에 넣은 값이 채워지며 상수값, 또는 유형 값을 넣을 수 있다. value가 list인 경우 shape을 지정했다면 shape의 형태에 같거나 작아야한다.(shape의 형태보다 작으면 마지막 값으로 그 형태를 채우게 됨) shape은 선택사항으로, 선택하지 않는 경우 value 형태를 따른다. dtype을 지정하지 않을 시 유추한다.
매개변수
- value : 출력 유형의 상수 값 또는 list
- dtype: 결과값의 데이터타입
- shape: 결과 텐서의 선택적인 치수
- name: 이름
- verify_shape: 값의 형태를 검증할 수 있는 boolean 값
예시:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
constant_1 = tf.constant([1,2,3,4,5] ,dtype=tf.float32) constant_2 = tf.constant([1,2,3], shape=(1,4), dtype=tf.float32) constant_3 = tf.constant(-1.0, shape=[2,3] ,dtype=tf.float32) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print(sess.run(constant_1)) print(sess.run(constant_2)) print(sess.run(constant_3)) |

tf.Variable
매개변수 엄청 많음.
중요한 것은 Variable은 반드시 초기화를 시켜야한다.
|
1 2 3 4 5 6 7 8 |
a = tf.Variable([1,2,3], dtype=tf.float32, name='a') init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print(a.eval()) |
출력 [1,2,3]
tf.placeholder(dtype, shape=None,name=None)
|
1 2 3 4 5 6 7 |
x = tf.placeholder(tf.float32, shape=(1024,1024)) y = tf.matmul(x,x) with tf.Session() as sess: rand_array = np.random.rand(1024,1024) print(sess.run(y, feed_dict={x : rand_array})) |
값을 받아야하는 컨테이너 박스처럼 생각하면 편리하다. 그러므로 값을 받아야 연산을 처리할 수 있으므로 받는 함수인 feed_dict 를 반드시 사용해야 한다.