GPU 사용하기 https://tensorflowkorea.gitbooks.io/tensorflow-kr/content/g3doc/how_tos/using_gpu/
여러 개의 GPU를 사용하여 모델을 훈련하는 과정의 좋은 예 : cifar10 튜토리얼
An introduction to TensorFlow queuing and threading : [link]
- 변수공유 [link]
- tf.get_variable(<name>, <shape>, <initializer>): 입력된 이름의 변수를 생성하거나 반환합니다.
- tf.constant_initializer(value) 제공된 값으로 모든 것을 초기화합니다,
- tf.random_uniform_initializer(a, b) [a, b]를 균일하게 초기화 합니다,
- tf.random_normal_initializer(mean, stddev) 주어진 평균 및 표준 편차로 정규 분포에서 초기화합니다.
- def conv_relu(input, kernel_shape, bias_shape):
# Create variable named "weights".
weights = tf.get_variable("weights", kernel_shape, initializer=tf.random_normal_initializer())
# Create variable named "biases".
biases = tf.get_variable("biases", bias_shape, initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(input, weights, strides=[1, 1, 1, 1], padding='SAME')
return tf.nn.relu(conv + biases)
- tf.variable_scope(<scope_name>): Manages namespaces for names passed to tf.get_variable()
- def my_image_filter(input_images):
with tf.variable_scope("conv1"):
# Variables created here will be named "conv1/weights", "conv1/biases".
relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
with tf.variable_scope("conv2"):
# Variables created here will be named "conv2/weights", "conv2/biases".
return conv_relu(relu1, [5, 5, 32, 32], [32])
- 오류 사용 예
result1 = my_image_filter(image1)
result2 = my_image_filter(image2)
# Raises ValueError(... conv1/weights already exists ...)
- 공유 사용 예
with tf.variable_scope("image_filters") as scope:
result1 = my_image_filter(image1)
scope.reuse_variables()
result2 = my_image_filter(image2)
- 파라메터를 공유한 예제
- variable_scope를 이용한 예제
- tf.get_variable_scope() 현재 scope를 호출
- tf.get_variable_scope().reuse == False
- tf.get_variable_scope().reuse == True <=tf.get_variable_scope().reuse_variables()
import tensorflow as tf
import numpy as np
# Collection keys
print("### Collection keys ###")
print (dir(tf.GraphKeys))
"""
['ACTIVATIONS', 'ASSET_FILEPATHS', 'BIASES', 'CONCATENATED_VARIABLES', 'COND_CONTEXT', 'EVAL_STEP', 'GLOBAL_STEP', 'GLOBAL_VARIABLES', 'INIT_OP', 'LOCAL_INIT_OP', 'LOCAL_RESOURCES', 'LOCAL_VARIABLES', 'LOSSES', 'METRIC_VARIABLES', 'MODEL_VARIABLES', 'MOVING_AVERAGE_VARIABLES', 'QUEUE_RUNNERS', 'READY_FOR_LOCAL_INIT_OP', 'READY_OP', 'REGULARIZATION_LOSSES', 'RESOURCES', 'SAVEABLE_OBJECTS', 'SAVERS', 'SUMMARIES', 'SUMMARY_OP', 'TABLE_INITIALIZERS', 'TRAINABLE_RESOURCE_VARIABLES', 'TRAINABLE_VARIABLES', 'TRAIN_OP', 'UPDATE_OPS', 'VARIABLES', 'WEIGHTS', 'WHILE_CONTEXT', '_STREAMING_MODEL_PORTS', '_SUMMARY_COLLECTION', '_VARIABLE_COLLECTIONS', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_tf_api_names']
"""
# Create Varialbes and show the colllections again
x = tf.Variable(np.random.rand(5, 5, 5), dtype=tf.float32, name="x") # 'x:0'
y = tf.get_variable("y",[5,5,5],dtype=tf.float32, # 'y:0'
initializer=tf.random_normal_initializer())
global_step = tf.get_variable( # 'global_step:0'
'global_step', [],
initializer=tf.constant_initializer(0), trainable=False)
c = tf.constant(np.random.rand(5, 5, 5), name="c") # 'c:0'
z = x+y # 'add:0'
w = tf.get_variable("w", shape=(5, 5), initializer=tf.random_normal_initializer())
init_op = tf.global_variables_initializer() #.initialize_all_variables()
# Show values in VARIABLES collections
print("GLOBAL_VARIABLES", tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES))
print("TRAINABLE_VARIABLES", tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES))
print( "TABLE_INITIALIZERS", tf.get_collection(tf.GraphKeys.TABLE_INITIALIZERS))
print( "SUMMARIES", tf.get_collection(tf.GraphKeys.SUMMARIES))
print( "QUEUE_RUNNERS", tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS))
"""
GLOBAL_VARIABLES [<tf.Variable 'x:0' shape=(5, 5, 5) dtype=float32_ref>,
<tf.Variable 'y:0' shape=(5, 5, 5) dtype=float32_ref>,
<tf.Variable 'global_step:0' shape=() dtype=float32_ref>,
<tf.Variable 'w:0' shape=(5, 5) dtype=float32_ref>]
TRAINABLE_VARIABLES [<tf.Variable 'x:0' shape=(5, 5, 5) dtype=float32_ref>,
<tf.Variable 'y:0' shape=(5, 5, 5) dtype=float32_ref>,
<tf.Variable 'w:0' shape=(5, 5) dtype=float32_ref>]
TABLE_INITIALIZERS []
SUMMARIES []
QUEUE_RUNNERS []
"""
tf.add_to_collection("sample", x) #collection sample=['x:0']
tf.add_to_collection("sample", y) #collection sample=['x:0','y:0']
tf.add_to_collection("sample", z) #collection sample=['x:0','y:0','add:0']
print( "sample : ",tf.get_collection("sample"))
print( "sampleaxx : ",tf.get_collection("sampleadd"))
with tf.name_scope("name_scope") as scope:
print('aa',tf.get_collection("sample", scope)) # name_scope/sample=[]
a=tf.Variable(0,name='a') #name_scope/a:0
b=tf.get_variable('x',[]) #'x_1:0'
tf.add_to_collection("sample", a) #collection sample=['x:0','y:0','add:0','name_scope/a:0']
tf.add_to_collection("sample", b) #collection sample=['x:0','y:0','add:0','name_scope/a:0','x_1:0']
print( "sample : ", tf.get_collection("sample"))
#sample : [<tf.Variable 'x:0' shape=(5, 5, 5) dtype=float32_ref>,
# <tf.Variable 'y:0' shape=(5, 5, 5) dtype=float32_ref>,
# <tf.Tensor 'add:0' shape=(5, 5, 5) dtype=float32>,
# <tf.Variable 'name_scope/a:0' shape=() dtype=int32_ref>,
# <tf.Variable 'x_1:0' shape=() dtype=float32_ref>]
print( tf.get_collection("sample", scope))
#[<tf.Variable 'name_scope/a:0' shape=() dtype=int32_ref>]
print( len(tf.get_collection("sample"))) #5
print( len(tf.get_collection("sample", scope))) #1
print( x.name) #'x:0'
print( y.name) #'y:0'
print( z.name)#'add:0'
print( a.name)#''name_scope/a:0'
with tf.variable_scope('v_scope') : #tf.get_variable_scope()):
for i in range(2):
with tf.device('/gpu:%d' % i):
with tf.name_scope('%s_%d' % ('tower', i)) as scope:
p=tf.get_variable('p',[]) #name:'v_scope/p:0', sharedname:'v_scope/p', device:'/device:GPU:0'
tf.add_to_collection('aaa',p)
tf.get_variable_scope().reuse_variables()
print(tf.get_collection('aaa')) #[<tf.Variable 'v_scope/p:0' shape=() dtype=float32_ref>]
print(tf.get_collection('v_scope'))