Python tensorflow使用GPU与CPU计算损耗
当我用GPU和CPU计算损耗时,我对损耗计算中的差异有点困惑 模型为6层CNN 从检查点加载模型并使用相同的数据运行它。用CPU和GPU计算损耗 CPU损耗:0.4687191 GPU损失:0.46873742 有人能给我解释一下为什么这些损失的计算方法不同吗Python tensorflow使用GPU与CPU计算损耗,python,tensorflow,Python,Tensorflow,当我用GPU和CPU计算损耗时,我对损耗计算中的差异有点困惑 模型为6层CNN 从检查点加载模型并使用相同的数据运行它。用CPU和GPU计算损耗 CPU损耗:0.4687191 GPU损失:0.46873742 有人能给我解释一下为什么这些损失的计算方法不同吗 #WITH CPU! - testing cpu vs cpu optimizer calculations import time tf.reset_default_graph() new_saver = tf.train.imp
#WITH CPU! - testing cpu vs cpu optimizer calculations
import time
tf.reset_default_graph()
new_saver = tf.train.import_meta_graph('./graph/final.meta')
with tf.Session() as sess:
new_saver.restore(sess, tf.train.latest_checkpoint('./tmp'))
optimize = tf.get_default_graph().get_operation_by_name( "optimizer" )
c_loss = sess.run('loss:0', feed_dict={'x:0': x_train[0:128], 'y:0': y_train[0:128], 'is_training:0': True})
print('initial: c_loss', c_loss)
sess.run(optimize, feed_dict={'x:0': x_train[0:128], 'y:0': y_train[0:128], 'is_training:0': True})
c_loss = sess.run('loss:0', feed_dict={'x:0': x_train[0:128], 'y:0': y_train[0:128], 'is_training:0': True})
print('post: c_loss', c_loss)
输出:首字母:c_损失0.4687191
帖子:c_损失0.5455321
#WITH GPU! - testing cpu vs gpu optimizer calculations
import time
tf.reset_default_graph()
new_saver = tf.train.import_meta_graph('gdrive/My Drive/graph/final.meta')
with tf.Session() as sess:
new_saver.restore(sess, tf.train.latest_checkpoint('gdrive/My Drive/tmp'))
optimize = tf.get_default_graph().get_operation_by_name( "optimizer" )
c_loss = sess.run('loss:0', feed_dict={'x:0': x_train[0:128], 'y:0': y_train[0:128], 'is_training:0': True})
print('initial: c_loss', c_loss)
sess.run(optimize, feed_dict={'x:0': x_train[0:128], 'y:0': y_train[0:128], 'is_training:0': True})
c_loss = sess.run('loss:0', feed_dict={'x:0': x_train[0:128], 'y:0': y_train[0:128], 'is_training:0': True})
print('post: c_loss', c_loss)
输出:首字母:c_损失0.46873742
职位:c_损失0.5432756
编辑:
也。我用两个不同的CPU会话加载模型,发现上面的损耗计算是相同的。只有当我使用GPU计算损失时,它们才会有所不同。如果你对训练模型的数据进行洗牌,它们可能会有所不同。尝试修复代码上方的
numpy.random.seed(123)
(但在导入之后),然后禁用洗牌,损失应该相同
例如,tensorlayers
mini批处理生成器允许您将shuffle参数设置为False
import numpy as np
import tensorlayers as tl
X = np.asarray([['a','a'], ['b','b'], ['c','c'], ['d','d'], ['e','e'], ['f','f']])
y = np.asarray([0,1,2,3,4,5])
for batch in tl.iterate.minibatches(inputs=X, targets=y, batch_size=16, shuffle=False):
print(batch)
我没有做任何洗牌或任何形式的正确配料。我添加了上面的代码,所以它应该更有意义。训练点到adamoptimizer。最小化()和损失点以减少平均值(交叉熵)。cpu计算可以在float64上进行,gpu计算可以在float32上进行吗?你能打印形状吗?