import tensorflow as tf
import tensorflow.contrib.slim as slim
# Model Variables
weights = slim.model_variable('weights',
shape=[5, 5, 3 , 3],
initializer=tf.truncated_normal_initializer(stddev=0.1),
regularizer=slim.l2_regularizer(0.05))
model_variables = slim.get_model_variables()
# Regular variables
my_var = slim.variable('my_var',
shape=[20, 1],
initializer=tf.zeros_initializer())
regular_variables_and_model_variables = slim.get_variables()
print(weights.name)
print(my_var.name)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
a=sess.run(model_variables)
b=sess.run(regular_variables_and_model_variables)
print(b)
输出:
weights:0
my_var:0
[array([[[[-0.16223504, -0.12132157, 0.10163851],
[ 0.08857495, -0.07425972, 0.03480111],
[-0.01969922, -0.04821443, -0.05179226]],
[[-0.00419623, -0.03276922, 0.03275175],
[-0.025644 , -0.00346689, 0.11299219],
[-0.07914045, -0.06330657, -0.06027368]],
[[ 0.00671485, -0.03443812, -0.10506791],
[ 0.0589137 , -0.01653036, 0.08432978],
[ 0.06139712, 0.11145692, -0.00254891]],
[[-0.03500391, 0.09125845, 0.07176967],
[ 0.17051116, -0.04667612, -0.00772954],
[ 0.05177089, -0.08427192, -0.08593903]],
[[-0.06125113, -0.11570779, 0.05441874],
[-0.13249822, 0.0278293 , 0.01567566],
[-0.12261826, 0.04078599, -0.01024829]]],
[[[ 0.16522811, 0.03671779, -0.04059736],
[ 0.03879705, -0.08208816, -0.19011341],
[ 0.02503098, 0.06463507, 0.16909577]],
[[ 0.0926263 , -0.02539631, -0.09377422],
[ 0.12903316, -0.13303475, 0.07472865],
[-0.01440961, 0.06500827, 0.08350212]],
[[ 0.03451673, -0.06978918, -0.1250405 ],
[-0.16495989, -0.10512201, 0.0095012 ],
[ 0.02511693, 0.08020766, 0.14752094]],
[[-0.05590098, 0.09556431, 0.09903727],
[-0.0144945 , -0.05353352, 0.06713809],
[ 0.00200485, 0.0611572 , -0.15032347]],
[[-0.15349977, -0.09721393, -0.03079793],
[-0.15261695, -0.04597082, -0.01434774],
[ 0.0412796 , -0.03227698, 0.09402122]]],
[[[ 0.13655595, 0.04866274, -0.02277255],
[ 0.09591573, 0.04081535, -0.00743454],
[-0.10405406, -0.01215897, 0.07217003]],
[[ 0.16204494, 0.00646965, -0.1247283 ],
[ 0.03792977, -0.0629625 , 0.02341435],
[ 0.01753754, -0.02816099, -0.02612311]],
[[ 0.06214956, -0.07752737, -0.12687491],
[-0.03588537, -0.02672707, 0.04545514],
[ 0.10898466, -0.11097177, -0.00490333]],
[[-0.10032804, 0.08317929, -0.17206025],
[-0.02717291, -0.00503827, -0.13393812],
[-0.0601451 , 0.01827372, 0.17303394]],
[[-0.0121774 , -0.0454082 , -0.03686107],
[-0.07822569, -0.08697627, -0.02149115],
[-0.00982824, -0.02452661, -0.11567926]]],
[[[-0.11132729, 0.0020334 , 0.00435622],
[-0.04156205, -0.09458689, 0.14010975],
[-0.0100046 , -0.05354413, -0.02195279]],
[[-0.07163369, 0.01238955, 0.01008388],
[ 0.03819063, -0.03718038, 0.07996228],
[ 0.03692595, -0.08231624, 0.04104179]],
[[-0.0037325 , 0.08111983, -0.0533362 ],
[ 0.09269632, -0.09082448, -0.11727528],
[ 0.04807084, -0.11332928, -0.07088885]],
[[-0.1190962 , 0.01177804, 0.04152609],
[ 0.09272311, -0.07375132, -0.09496 ],
[ 0.04162823, -0.07212376, -0.04184552]],
[[ 0.03388771, 0.16835983, 0.04832568],
[ 0.04424911, 0.17199731, 0.10929054],
[-0.13152617, 0.01925231, 0.10731445]]],
[[[-0.04271707, 0.04924161, -0.05562565],
[ 0.09799628, 0.02938068, 0.04023891],
[-0.04082066, 0.03450542, 0.00169373]],
[[ 0.04776294, -0.0516752 , 0.08142476],
[-0.10328829, -0.00172146, 0.00892421],
[-0.0362827 , 0.08838445, 0.1529492 ]],
[[ 0.17458294, 0.10121899, -0.07531347],
[ 0.05691049, -0.1580013 , -0.08725848],
[ 0.17685421, -0.08303452, -0.0510377 ]],
[[ 0.04932152, 0.07331303, 0.03761637],
[-0.0706702 , 0.04057423, 0.01305293],
[ 0.07592862, 0.04834391, 0.01235934]],
[[ 0.05375633, -0.15525563, -0.15755388],
[ 0.01247952, -0.0186142 , 0.08487248],
[ 0.06962581, 0.04253736, -0.01634468]]]], dtype=float32), array([[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.]], dtype=float32)]
slim是苗条的意思,slim.get_variables()把model_variable也得到了。