and assign speed comparison placeholder

In the CPU, and using variable placeholder similar results
on the GPU, each time using the variable transmission placeholder faster than 3: 2
using the GPU main bottleneck that copy operations between memory and the GPU

"""
place_holder和variable速度对比
"""
import time

import numpy as np
import tensorflow as tf

M = 4096
N = 4096
K = 4096
A = np.random.random((N, M))
B = np.random.random((M, K))
a = tf.placeholder(dtype=tf.float32, shape=(None, M))
b = tf.placeholder(dtype=tf.float32, shape=(None, N))
c = tf.Variable(initial_value=A, dtype=tf.float32)
pro = a @ b
use_assign = c @ b
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    beg_time = time.time()
    for i in range(5):
        sess.run(use_assign, feed_dict={
            b: B
        })
    print("use variable", time.time() - beg_time)
    beg_time = time.time()
    for i in range(5):
        sess.run(pro, feed_dict={
            a: A,
            b: B
        })
    print("use placeholder", time.time() - beg_time)

Guess you like

Origin www.cnblogs.com/weiyinfu/p/11264639.html