如何计算tensorflow计算model的大小size(1)

import os
import tensorflow as tf
import tensorflow.contrib.slim as slim

CURRENT_DIR = os.getcwd()
train_dir  = CURRENT_DIR + '/logs/'

a = tf.placeholder(tf.float32, shape=[None, 6, 512], name='a')
print a.shape
b     = slim.fully_connected(a, 1 * 512, activation_fn=tf.nn.relu, weights_initializer=slim.variance_scaling_initializer(), scope='fc1')
print b.shape

for tv in tf.trainable_variables():
    print (tv.name)

w = tf.get_default_graph().get_tensor_by_name("fc1/weights:0")   
b = tf.get_default_graph().get_tensor_by_name("fc1/biases:0")

     
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    print(sess.run(b))
    print(sess.run(w))
    print(sess.run(b).shape)
    print(sess.run(w).shape)    
    print(type(sess.run(b)))
    print(type(sess.run(w)[0,0]))   
    checkpoint_path = os.path.join(train_dir,'abc.ckpt')
    saver = tf.train.Saver()
    saver.save(sess, checkpoint_path)
    print '*********    model saved    *********'  

(512,)
(512, 512)

计算方法

1,共:512×512+512=262,656(个参数)

2,每个参数是float32,占4个字节

共 262,656 × 4= 1,050,624(个字节)

3,1K=1024字节,1M=1024K

1,050,624 / 1024 / 1024=1.001953125M

4,以图为证:

如何计算tensorflow计算model的大小size(1)_第1张图片

你可能感兴趣的:(如何计算tensorflow计算model的大小size(1))