Tensorflow 我可以用谷歌Colab使用TensorBoard吗?
在Google Colab上训练TensorFlow模型时,有没有办法使用TensorBoard?以下是如何在Google Colab上内联显示模型的方法。下面是一个显示占位符的非常简单的示例:Tensorflow 我可以用谷歌Colab使用TensorBoard吗?,tensorflow,tensorboard,google-colaboratory,Tensorflow,Tensorboard,Google Colaboratory,在Google Colab上训练TensorFlow模型时,有没有办法使用TensorBoard?以下是如何在Google Colab上内联显示模型的方法。下面是一个显示占位符的非常简单的示例: from IPython.display import clear_output, Image, display, HTML import tensorflow as tf import numpy as np from google.colab import files def strip_cons
from IPython.display import clear_output, Image, display, HTML
import tensorflow as tf
import numpy as np
from google.colab import files
def strip_consts(graph_def, max_const_size=32):
"""Strip large constant values from graph_def."""
strip_def = tf.GraphDef()
for n0 in graph_def.node:
n = strip_def.node.add()
n.MergeFrom(n0)
if n.op == 'Const':
tensor = n.attr['value'].tensor
size = len(tensor.tensor_content)
if size > max_const_size:
tensor.tensor_content = "<stripped %d bytes>"%size
return strip_def
def show_graph(graph_def, max_const_size=32):
"""Visualize TensorFlow graph."""
if hasattr(graph_def, 'as_graph_def'):
graph_def = graph_def.as_graph_def()
strip_def = strip_consts(graph_def, max_const_size=max_const_size)
code = """
<script>
function load() {{
document.getElementById("{id}").pbtxt = {data};
}}
</script>
<link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
<div style="height:600px">
<tf-graph-basic id="{id}"></tf-graph-basic>
</div>
""".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))
iframe = """
<iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe>
""".format(code.replace('"', '"'))
display(HTML(iframe))
"""Create a sample tensor"""
sample_placeholder= tf.placeholder(dtype=tf.float32)
"""Show it"""
graph_def = tf.get_default_graph().as_graph_def()
show_graph(graph_def)
从IPython.display导入清除输出、图像、显示、HTML
导入tensorflow作为tf
将numpy作为np导入
从google.colab导入文件
定义带常数(图形定义,最大常数大小=32):
“”“从图形_def中删除大常量值。”“”
strip_def=tf.GraphDef()
对于图形_def.node中的n0:
n=strip_def.node.add()
n、 合并自(n0)
如果n.op=='Const':
张量=n.attr['value'].张量
大小=len(张量。张量内容)
如果大小>最大常量大小:
tensor.tensor_content=”“%大小
返回条
def显示图(图def,最大常量大小=32):
“”“可视化TensorFlow图形。”“”
如果hasattr(graph_def,‘as_graph_def’):
graph_def=graph_def.as_graph_def()
条带定义=条带常量(图形定义,最大常量大小=最大常量大小)
代码=”“
函数加载(){{
document.getElementById(“{id}”).pbtxt={data};
}}
.format(data=repr(str(strip_def)),id='graph'+str(np.random.rand())
iframe=”“”
“”格式(代码.replace(“,”))
显示(HTML(iframe))
“”“创建示例张量”“”
示例_占位符=tf.placeholder(dtype=tf.float32)
“展示它”
graph_def=tf.get_default_graph().as_graph_def()
显示图形(图形定义)
目前,您无法在Google Colab上以本地运行的方式运行Tensorboard服务。此外,您不能通过类似于
summary\u writer=tf.summary.FileWriter('./logs',graph\u def=sess.graph\u def)的方式将整个日志导出到驱动器中
,这样您就可以下载日志并在本地查看。编辑:您可能想试一试,从TensorFlow 1.13开始提供
在
%tensorboard
magic存在之前
实现这一点的方法是使用
. 可以找到一个Colab示例
以下是步骤(代码片段表示colab中“code”类型的单元格):
灵感来自
LOG_DIR='/tmp/LOG'
获取_ipython().system_原始(
'tensorboard--logdir{}--主机0.0.0.0--端口6006&'
.格式(日志目录)
)
将传递到
wget
的链接替换为适用于您的操作系统的正确下载链接
!wgethttps://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
! 解压ngrok-stable-linux-amd64.zip
get_ipython().system_raw('./ngrok http 6006&'))
…并检索公共url。
!curl-shttp://localhost:4040/api/tunnels |蟒蛇3-c\
“导入sys,json;打印(json.load(sys.stdin)['tunnels'][0]['public_url'])”
这里有一个更简单的方法在Google Colab上实现相同的ngrok隧道方法
!pip install tensorboardcolab
那么
假设您正在使用Keras:
model.fit(......,callbacks=[TensorBoardColabCallback(tbc)])
您可以阅读原始帖子。TensorBoard for TensorFlow使用TensorBoardLab在Google Colab上运行。这在内部使用ngrok进行隧道开挖
!pip安装TensorBoardLab
tbc=TensorBoardColab()
这将自动创建一个可使用的张力板链接。此张力板正在读取“/Graph”处的数据
summary\u writer=tbc.get\u writer()
TensorBoardLab库具有返回指向“./Graph”位置上方的FileWriter对象的方法
参考资料:我利用谷歌硬盘的备份和同步功能。培训期间预先保存在我的谷歌硬盘中的事件文件会自动同步到我自己电脑上的文件夹中。让我们调用此文件夹
logs
。要访问tensorboard中的可视化,我打开命令提示符,导航到同步的google drive文件夹,然后键入:tensorboard--logdir=logs
因此,通过自动将驱动器与计算机同步(使用备份和同步),我可以像在自己的计算机上训练一样使用tensorboard
编辑:
这是一本可能有用的笔记本 我试过了,但没有得到结果,但当按如下方式使用时,得到了结果
import tensorboardcolab as tb
tbc = tb.TensorBoardColab()
在此之后,从输出打开链接
import tensorflow as tf
import numpy as np
显式创建图形对象
graph = tf.Graph()
with graph.as_default()
完整示例:
with tf.name_scope("variables"):
# Variable to keep track of how many times the graph has been run
global_step = tf.Variable(0, dtype=tf.int32, name="global_step")
# Increments the above `global_step` Variable, should be run whenever the graph is run
increment_step = global_step.assign_add(1)
# Variable that keeps track of previous output value:
previous_value = tf.Variable(0.0, dtype=tf.float32, name="previous_value")
# Primary transformation Operations
with tf.name_scope("exercise_transformation"):
# Separate input layer
with tf.name_scope("input"):
# Create input placeholder- takes in a Vector
a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")
# Separate middle layer
with tf.name_scope("intermediate_layer"):
b = tf.reduce_prod(a, name="product_b")
c = tf.reduce_sum(a, name="sum_c")
# Separate output layer
with tf.name_scope("output"):
d = tf.add(b, c, name="add_d")
output = tf.subtract(d, previous_value, name="output")
update_prev = previous_value.assign(output)
# Summary Operations
with tf.name_scope("summaries"):
tf.summary.scalar('output', output) # Creates summary for output node
tf.summary.scalar('product of inputs', b, )
tf.summary.scalar('sum of inputs', c)
# Global Variables and Operations
with tf.name_scope("global_ops"):
# Initialization Op
init = tf.initialize_all_variables()
# Collect all summary Ops in graph
merged_summaries = tf.summary.merge_all()
# Start a Session, using the explicitly created Graph
sess = tf.Session(graph=graph)
# Open a SummaryWriter to save summaries
writer = tf.summary.FileWriter('./Graph', sess.graph)
# Initialize Variables
sess.run(init)
def run_graph(input_tensor):
"""
Helper function; runs the graph with given input tensor and saves summaries
"""
feed_dict = {a: input_tensor}
output, summary, step = sess.run([update_prev, merged_summaries, increment_step], feed_dict=feed_dict)
writer.add_summary(summary, global_step=step)
# Run the graph with various inputs
run_graph([2,8])
run_graph([3,1,3,3])
run_graph([8])
run_graph([1,2,3])
run_graph([11,4])
run_graph([4,1])
run_graph([7,3,1])
run_graph([6,3])
run_graph([0,2])
run_graph([4,5,6])
# Writes the summaries to disk
writer.flush()
# Flushes the summaries to disk and closes the SummaryWriter
writer.close()
# Close the session
sess.close()
# To start TensorBoard after running this file, execute the following command:
# $ tensorboard --logdir='./improved_graph'
有另一种解决方案,但我们必须使用TFv2.0预览。因此,如果迁移没有问题,请尝试以下方法: 为GPU或CPU安装tfv2.0(TPU尚未可用) CPU
tf-nightly-2.0-preview
GPU
tf-nightly-gpu-2.0-preview
%%capture
!pip install -q tf-nightly-gpu-2.0-preview
# Load the TensorBoard notebook extension
# %load_ext tensorboard.notebook # For older versions
%load_ext tensorboard
照常导入张力板:
从tensorflow.keras.callbacks导入TensorBoard
清理或创建用于保存日志的文件夹(在运行培训fit()
之前运行此行)
玩TensorBoard吧!:)
%tensorboard--logdir logs/fit
# Clear any logs from previous runs
import time
!rm -R ./logs/ # rf
log_dir="logs/fit/{}".format(time.strftime("%Y%m%d-%H%M%S", time.gmtime()))
tensorboard = TensorBoard(log_dir=log_dir, histogram_freq=1)
官方的colab笔记本和github上的
新的TFv2.0 alpha版本:
CPU!pip安装-q tensorflow==2.0.0-alpha0
GPU!pip安装-q tensorflow gpu==2.0.0-alpha0
要加入@solver149 answer,下面是一个如何在google colab中使用TensorBoard的简单示例
1.创建图形,例如:
2.安装张力板
==>在我的案例中的结果:
Requirement already satisfied: tensorboardcolab in /usr/local/lib/python3.6/dist-packages (0.0.22)
3.使用它:)
首先从tensorboaedcolab导入TensorBoard(您可以使用a = tf.constant(3.0, dtype=tf.float32)
b = tf.constant(4.0)
total = a + b
!pip install tensorboardcolab # to install tensorboeadcolab if it does not it not exist
Requirement already satisfied: tensorboardcolab in /usr/local/lib/python3.6/dist-packages (0.0.22)
from tensorboardcolab import *
tbc = TensorBoardColab() # To create a tensorboardcolab object it will automatically creat a link
writer = tbc.get_writer() # To create a FileWriter
writer.add_graph(tf.get_default_graph()) # add the graph
writer.flush()
Using TensorFlow backend.
Wait for 8 seconds...
TensorBoard link:
http://cf426c39.ngrok.io
# in case of CPU, you can this line
# !pip install -q tf-nightly-2.0-preview
# in case of GPU, you can use this line
!pip install -q tf-nightly-gpu-2.0-preview
# %load_ext tensorboard.notebook # not working on 22 Apr
%load_ext tensorboard # you need to use this line instead
import tensorflow as tf
# show tensorboard
%tensorboard --logdir logs/fit
!pip install tensorflow==2.0.0-alpha0
%load_ext tensorboard.notebook
!wget https://raw.githubusercontent.com/hse-aml/intro-to- dl/master/setup_google_colab.py -O setup_google_colab.py
import setup_google_colab
import os
os.system("tensorboard --logdir=./logs --host 0.0.0.0 --port 6006 &")
setup_google_colab.expose_port_on_colab(6006)
Open https://a1b2c34d5.ngrok.io to access your 6006 port
https://github.com/MUmarAmanat/MLWithTensorflow/blob/master/colab_tensorboard.ipynb
%load_ext tensorboard.notebook
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
%tensorboard — logdir logs
%load_ext tensorboard
%tensorboard --logdir logs
%load_ext tensorboard
%tensorboard --logdir=./logs
!pip install tensorflow==2.0
import tensorflow as tf
# The function to be traced.
@tf.function
def my_func(x, y):
# A simple hand-rolled layer.
return tf.nn.relu(tf.matmul(x, y))
# Set up logging.
logdir = './logs/func'
writer = tf.summary.create_file_writer(logdir)
# Sample data for your function.
x = tf.random.uniform((3, 3))
y = tf.random.uniform((3, 3))
# Bracket the function call with
# tf.summary.trace_on() and tf.summary.trace_export().
tf.summary.trace_on(graph=True, profiler=True)
# Call only one tf.function when tracing.
z = my_func(x, y)
with writer.as_default():
tf.summary.trace_export(
name="my_func_trace",
step=0,
profiler_outdir=logdir)
%load_ext tensorboard
%tensorboard --logdir ./logs/func
%load_ext tensorboard
import datetime
logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
model.fit(x=x_train,
y=y_train,
epochs=5,
validation_data=(x_test, y_test),
callbacks=[tensorboard_callback])
%load_ext tensorboard
!rm -rf ./logs/ #to delete previous runs
%tensorboard --logdir logs/
tensorboard = TensorBoard(log_dir="./logs")
model.fit(X_train, y_train, epochs = 1000,
callbacks=[tensorboard], validation_data=(X_test, y_test))
%load_ext tensorboard
%tensorboard --logdir /content/logs
/content/logs