标定张量流图的正确方法是什么?

问题描述 投票:2回答:1

我想对图形的某些部分进行基准测试,为简单起见,这里我使用的只是[conv3x3]。conv_block

  1. 循环中使用的x_np是否相同,还是每次都需要重新生成它?
  2. 我需要在运行实际基准测试之前进行一些“热身”运行(似乎需要在GPU上进行基准测试吗?怎么做呢? sess.run(tf.global_variables_initializer())是否足够?
  3. 在python中测量时间的正确方法是什么,即更精确的方法。
  4. 我需要在运行脚本之前重置Linux上的某些系统缓存(也许禁用np.random.seed足够了吗?]
  5. 示例代码:

import os
import time

import numpy as np
import tensorflow as tf

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

np.random.seed(2020)


def conv_block(x, kernel_size=3):
    # Define some part of graph here

    bs, h, w, c = x.shape
    in_channels = c
    out_channels = c

    with tf.variable_scope('var_scope'):
        w_0 = tf.get_variable('w_0', [kernel_size, kernel_size, in_channels, out_channels], initializer=tf.contrib.layers.xavier_initializer())
        x = tf.nn.conv2d(x, w_0, [1, 1, 1, 1], 'SAME')

    return x


def get_data_batch(spatial_size, n_channels):
    bs = 1
    h = spatial_size
    w = spatial_size
    c = n_channels

    x_np = np.random.rand(bs, h, w, c)
    x_np = x_np.astype(np.float32)
    #print('x_np.shape', x_np.shape)

    return x_np


def run_graph_part(f_name, spatial_size, n_channels, n_iter=100):
    print('=' * 60)
    print(f_name.__name__)

    tf.reset_default_graph()
    with tf.Session() as sess:
        x_tf = tf.placeholder(tf.float32, [1, spatial_size, spatial_size, n_channels], name='input')
        z_tf = f_name(x_tf)
        sess.run(tf.global_variables_initializer())

        x_np = get_data_batch(spatial_size, n_channels)
        start_time = time.time()
        for _ in range(n_iter):
            z_np = sess.run(fetches=[z_tf], feed_dict={x_tf: x_np})[0]
        avr_time = (time.time() - start_time) / n_iter
        print('z_np.shape', z_np.shape)
        print('avr_time', round(avr_time, 3))

        n_total_params = 0
        for v in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='var_scope'):
            n_total_params += np.prod(v.get_shape().as_list())
        print('Number of parameters:', format(n_total_params, ',d'))


if __name__ == '__main__':
    run_graph_part(conv_block, spatial_size=128, n_channels=32, n_iter=100)

我想对图的某些部分进行基准测试,为简单起见,这里我使用的只是conv3x3的conv_block。可以在循环中使用x_np相同吗,还是每次都需要重新生成它?我需要...

python linux tensorflow time benchmarking
1个回答
0
投票

对您的主要问题的回答,'对张量流图的基准进行基准测试的正确方法是什么?:

© www.soinside.com 2019 - 2024. All rights reserved.