我想用Keras查看TensorBoard中的CPU /内存使用情况。 为此,我需要执行add_run_metadata的方法。 但我找不到在Keras的TensorBoard回调中传递add_run_metadata方法的方法。 这是为Keras实现CPU /内存使用的好方法。
参考
编辑:我遇到了同样的问题。我正在编辑以分享我试图解决的问题。
我更改了keras源:callbacks.py,并将此line in on_epoch_end()替换为 -
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
result = self.sess.run([self.merged], feed_dict=feed_dict, options=run_options, run_metadata=run_metadata)
self.writer.add_run_metadata(run_metadata, 'epoch%d_step%d' % (epoch, i))
但是我最终得到以下错误:
...\tensorflow\stream_executor\dso_loader.cc:141] Couldn't open CUDA library cupti64_90.dll
...\tensorflow/stream_executor/lib/statusor.h:212] Non-OK-status: status_ status: Failed precondition: could not dlopen DSO: cupti64_90.dll; dlerror: cupti64_90.dll not found
令我感到困惑的是seems to be related正确安装了cuda,而且与我所做的改变没有任何明显的关联方式。
我正在使用keras版本2.1.6和tensorflow版本1.6.0
答案 0 :(得分:1)
解决方案是在TF会话中运行Keras模型,该解决方案基于博客文章keras-as-a-simplified-interface-to-tensorflow#calling-keras-layers-on-tensorflow-tensors。贝娄是一个详尽而完整的工作示例,
首先,虚拟生成数据:
data.py
import numpy as np
def load_data(n = 1000):
x = np.random.rand(n, 100)
y = np.sum(x, axis=1, keepdims=True)
return x, y
核心思想是在TF会话中运行模型,因此主要代码是纯TF,只有模型本身是用Keras定义的。为此(根据上述教程):
从keras.layers导入密集
model.py
def load_network(input_tensor):
x = Dense(100, activation='relu')(input_tensor)
x = Dense(100, activation='relu')(x)
x = Dense(1, activation='sigmoid')(x)
return x
运行keras模型(TensorBoard tutorial的干净但完整的版本)的TF会话:
run_runtime_stats.py
import tensorflow as tf
sess = tf.Session()
from keras import backend as K
from keras.objectives import mean_squared_error
K.set_session(sess)
from model import load_network
from data import load_data
# load your keras model as a tf.Tensor
input = tf.placeholder(tf.float32, shape=(None, 100)) # is passed as input to our keras layers
labels = tf.placeholder(tf.float32, shape=(None, 1))
net = load_network(input) # type(net) == tf.Tensor
loss = tf.reduce_mean(mean_squared_error(labels, net))
opt = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
writer = tf.summary.FileWriter(r'./logs', sess.graph)
sess.run(tf.global_variables_initializer())
with sess.as_default():
x, y = load_data(64)
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
sess.run([opt],
feed_dict={input: x, labels: y},
options=run_options,
run_metadata=run_metadata)
writer.add_run_metadata(run_metadata, 'runtime-statistics')
writer.close()