[AI]/python.tensorflow
tensorflow.basic
givemebro
2020. 5. 11. 12:49
반응형
import numpy as np
import tensorflow as tf
tensorflow에서의 data type
- tf.constant()
- tf.Variable()
- tf.placeholder()
tf.constant() # 상수 - 학습률 등 고정된 값
tf.Variable() # 변수 - 가중치(w)와 절편(b)
tf.placeholder() # 플레이스 홀더 - X값 등
> constant
a=tf.constant(2)
b=tf.constant(3)
display(a,b)
c=a*b
display(c)
sess=tf.Session()
display(sess.run(c))
sess.run([a,b,c])
sess.close()
<tf.Tensor 'Const_20:0' shape=() dtype=int32>
<tf.Tensor 'Const_21:0' shape=() dtype=int32>
<tf.Tensor 'mul_14:0' shape=() dtype=int32>
6
[2, 3, 6]
> Variable
a=tf.constant(2)
b=tf.Variable(3) # 3 : 초기값(반드시 넣어야함)
display(a,b)
c=a*b
sess=tf.Session()
sess.run(b.initializer)# 변수 초기화
display(sess.run(c))
<tf.Tensor 'Const_23:0' shape=() dtype=int32>
<tf.Variable 'Variable_5:0' shape=() dtype=int32_ref>
6
a=tf.constant([10,10,10])
b=tf.Variable([10,10,10])
c=a+b
sess=tf.Session()
sess.run(b.initializer)
sess.run(c)
array([20, 20, 20])
> placeholder
a=tf.constant(2)
b=tf.placeholder(tf.int32) # type을 설정
display(a,b)
c=a*b
sess=tf.Session()
sess.run(c,feed_dict={b:3}) #placeholder는 값을 지정해줘야한다.
# run 할때 마다 place holder에 값을 넣어야한다.
display(sess.run([a,b,c],feed_dict={b:3}))
display(sess.run([a,b,c],feed_dict={b:[3,1,2]})) # list도 가능하다
<tf.Tensor 'Const_28:0' shape=() dtype=int32>
<tf.Tensor 'Placeholder_11:0' shape=<unknown> dtype=int32>
[2, array(3), 6]
[2, array([3, 1, 2]), array([6, 2, 4])]
array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
[2, array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]]), array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16],
[18, 20, 22]])]
a=tf.constant(2)
b=tf.placeholder(tf.int32,shape=(None,3))# None으로 행의 갯수를 임의로 지정
c=a*b
sess=tf.Session()
display(sess.run(c,feed_dict={b:np.arange(9).reshape(3,3)}))
display(sess.run([a,b,c],feed_dict={b:np.arange(12).reshape(4,3)}))반응형