我使用tf估计器框架创建了一个CNN分类器模型。但是,我无法访问模型中定义的变量。tf.trainable_variables()总是返回0。如何使用tf估计器访问变量?具体地说,我如何获得参数总数的计数(将所有变量的维数加起来。
谢谢,哈罗德
发布于 2019-09-18 17:32:47
如上所述,您应该使用:
tf.estimator.Estimator.get_variable_names()为了得到所有的估计值,为了得到变量值有了变量后,您可以使用以下方法之一来获取估计器参数的总数。
numpy.prod相乘,然后求和:sum([np.prod(est.get_variable_value(var).shape) for var in est.get_variable_names()])
numpy.ndarray.size对变量大小求和,然后求和:sum([est.get_variable_value(var).size for var in est.get_variable_names()])
发布于 2019-05-31 14:47:03
您可以使用get_variable_names()获取所有变量名称,并使用get_variable_value(name)按名称获取变量值。
请像这样使用你的代码:
estimator = tf.estimator.Estimator(...)
params = estimator.get_variable_names()
for p in params:
print(p, estimator.get_variable_value(p).shape)更多的信息是https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator#get_variable_names和https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator#get_variable_value。
注意:您必须先创建图形,然后才能获取变量。
https://stackoverflow.com/questions/53149189
复制相似问题