Config parameter settings in 02_02Session



tensorflow TF AS Import
Import numpy NP AS

# TODO learning Session Config = parameters used tf.ConfigProto () a. GPU focus parameter related


DEF config_params ():
# TODO second way of performing the session
with tf.Graph () as_default ():.
# A, building the model in FIG
print ( 'default diagram of the current model is: {}' .format (tf.get_default_graph ()))
#. 1, the original definition of two input objects tensor
a tf.constant = (
value = [1,2,3,4,5,6,3,4,3,45 ,. 5], DTYPE = tf.float32, Shape = [. 3,. 5], name = 'A'
)
B tf.constant = (
value = [3,3,3,3,3,3234,56,324,3,5 ], DTYPE = tf.float32, Shape = [. 5,. 3]
)

# 2, using the above-described two op add constants were added a random number
v1 = a + np.random.random_sample ()
tf.add = V2 (B, tf.random_normal (Shape = [], DTYPE = tf.float32, 43 is SEED =))

#. 3, using two tensor matrix multiplication.
= tf.matmul rezult (V1, V2)
Print (A, B, V1, V2, rezult)

# two, building session
"" "
tf.Session (
target = '', a url given connection, only the operation of the distributed the time required for a given
graph = None, Photo call which, if not given, it calls the default map
config configuration protocol = None) session. ""
"
Optimizer = tf.OptimizerOptions (
do_common_subexpression_elimination = True, # represents the open public execution sub optimization sentence
do_constant_folding = True, # set to True open showing constant folding optimization.
opt_level = 0 # 0 is set to indicate on the above two optimization, the default is 0
)
= tf.GraphOptions graph_options (optimizer_options = Optimizer)
with tf.Session (config = tf.ConfigProto (graph_options = graph_options)) AS Sess:
Print (sess.run ([rezult, V2]))


DEF config_params1 ():
# Here TODO increased equipment tf.device specified operation or create a variable.
tf.Graph with () as_default ():.
# a, building the model in FIG
print (. 'FIG current default model is: {}' the format (tf.get_default_graph ()))
#. 1, define two of the original input Object tensor
with tf.device ( '/ the GPU: 0'):
# A and b two constants will gpu: 0 created.
tf.constant = A (
value = [1,2,3,4,5,6,3,4,3,45,5], DTYPE = tf.float32, Shape = [. 3,. 5], name = 'A '
)
B = tf.constant (
= value [3,3,3,3,3,3234,56,324,3,5], DTYPE = tf.float32, Shape = [. 5,. 3]
)

# 2, using the above-described two op add a constant by adding random number
V1 = A + np.random.random_sample ()
V2 = tf.add (B, tf.random_normal (Shape = [], DTYPE = tf.float32, 43 is SEED =))

#. 3, the use of two-phase matrix tensor multiply.
tf.device with ( '/ the GPU:. 1'):
rezult tf.matmul = (V1, V2)
Print (A, B, V1, V2, rezult)

# two, building session
"" "
tf.Session (
target = ' ', to the url given connection, only time distributed operating needs of a given
graph = None, Photo call which, if not given, it calls the default map
config configuration protocol = None) session.
"" "
tf.Session with (config = tf.ConfigProto (log_device_placement = True,
allow_soft_placement = True)) AS sess:
Print (sess.run ([rezult, v2]))


DEF config_params2 ():
# TODO parameters related to learning gpu.
tf.Graph with () as_default ():.
# a, building the model in FIG
print (. 'FIG current default model is: {}' the format (tf.get_default_graph ()))
#. 1, define two of the original input Object tensor
with tf.device ( '/ the GPU: 0'):
# A and b two constants will gpu: 0 created.
tf.constant = A (
value = [1,2,3,4,5,6,3,4,3,45,5], DTYPE = tf.float32, Shape = [. 3,. 5], name = 'A '
)
B = tf.constant (
= value [3,3,3,3,3,3234,56,324,3,5], DTYPE = tf.float32, Shape = [. 5,. 3]
)

# 2, using the above-described two op add a constant by adding random number
V1 = A + np.random.random_sample ()
V2 = tf.add (B, tf.random_normal (Shape = [], DTYPE = tf.float32, 43 is SEED =))

#. 3, the use of two-phase matrix tensor multiply.
tf.device with ( '/ the GPU:. 1'):
rezult tf.matmul = (V1, V2)
Print (A, B, V1, V2, rezult)

# two, building session
"" "
gpu_options parameters introduced
log_device_placement bool value the printing apparatus whether the log file at
allow_soft_placement bool value is used to allow dynamic tf cpu and gpu default False
"" "
gpu_options tf.GraphOptions = (
allow_growth = True, # does not use the entire pre-allocated memory gpu calculation, but from small to large demand increase
per_process_gpu_memory_fraction = 0.8 # value range (0, 1), limit the use of the percentage of the memory device gpu.
)
With tf.Session (config = tf.ConfigProto (log_device_placement = True,
allow_soft_placement = True,
gpu_options = gpu_options)) AS Sess:
Print (sess.run ([rezult, V2]))


IF the __name__ == '__main__':
config_params1 ()

Guess you like

Origin www.cnblogs.com/lvdongjie/p/11741747.html