placeholder和assign速度对比

在CPU上,使用variable和placeholder效果差不多
在GPU上,使用variable要比每次都传placeholder快得多3:2
使用GPU的瓶颈主要在于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)

转载于:https://www.cnblogs.com/weiyinfu/p/11264639.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值