Keras의 Tensorboard 콜백은 어떻게 사용합니까?
Keras와 신경망을 구축했습니다. Tensorboard로 데이터를 시각화하므로 다음을 활용했습니다.
keras.callbacks.TensorBoard(log_dir='/Graph', histogram_freq=0,
write_graph=True, write_images=True)
keras.io에 설명 된 대로 . 콜백을 실행할 때을 얻었 <keras.callbacks.TensorBoard at 0x7f9abb3898>
지만 "Graph"폴더에 파일이 없습니다. 이 콜백을 사용한 방식에 문제가 있습니까?
keras.callbacks.TensorBoard(log_dir='./Graph', histogram_freq=0,
write_graph=True, write_images=True)
이 라인은 콜백 텐서 보드 객체를 생성합니다. 해당 객체를 캡처 fit
하여 모델 의 기능에 제공해야합니다.
tbCallBack = keras.callbacks.TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)
...
model.fit(...inputs and parameters..., callbacks=[tbCallBack])
이렇게하면 콜백 객체를 함수에 제공했습니다. 훈련 중에 실행되며 tensorboard와 함께 사용할 수있는 파일을 출력합니다.
훈련 중에 생성 된 파일을 시각화하려면 터미널에서 실행하십시오.
tensorboard --logdir path_to_current_dir/Graph
도움이 되었기를 바랍니다 !
TensorBoard 콜백 을 사용하는 방법은 다음과 같습니다 .
from keras.callbacks import TensorBoard
tensorboard = TensorBoard(log_dir='./logs', histogram_freq=0,
write_graph=True, write_images=False)
# define model
model.fit(X_train, Y_train,
batch_size=batch_size,
epochs=nb_epoch,
validation_data=(X_test, Y_test),
shuffle=True,
callbacks=[tensorboard])
변화
keras.callbacks.TensorBoard(log_dir='/Graph', histogram_freq=0,
write_graph=True, write_images=True)
에
tbCallBack = keras.callbacks.TensorBoard(log_dir='Graph', histogram_freq=0,
write_graph=True, write_images=True)
그리고 당신의 모델을 설정
tbCallback.set_model(model)
터미널에서 실행
tensorboard --logdir Graph/
Keras 라이브러리로 작업 중이고 tensorboard를 사용하여 정확도 및 기타 변수의 그래프를 인쇄하려는 경우 다음 단계를 따르십시오.
1 단계 : 아래 명령을 사용하여 tensorboard를 가져 오려면 keras 콜백 라이브러리를 초기화하십시오.
from keras.callbacks import TensorBoard
2 단계 : "model.fit ()"명령 직전에 프로그램에 아래 명령을 포함 시키십시오.
tensor_board = TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)
참고 : "./graph"를 사용하십시오. "/ graph"를 사용하지 말고 현재 작업 디렉토리에 그래프 폴더를 생성합니다.
3 단계 : "model.fit ()"에 Tensorboard 콜백을 포함합니다. 샘플은 다음과 같습니다.
model.fit(X_train,y_train, batch_size=batch_size, epochs=nb_epoch, verbose=1, validation_split=0.2,callbacks=[tensor_board])
4 단계 : 코드를 실행하고 작업 디렉토리에 그래프 폴더가 있는지 확인하십시오. 위의 코드가 올바르게 작동하면 작업 디렉토리에 "Graph"폴더가 있습니다.
5 단계 : 작업 디렉토리에서 터미널을 열고 아래 명령을 입력하십시오.
tensorboard --logdir ./Graph
6 단계 : 이제 웹 브라우저를 열고 아래 주소를 입력하십시오.
http://localhost:6006
입력 한 후에는 다양한 변수의 그래프를 볼 수있는 Tensorbaord 페이지가 열립니다.
다음은 몇 가지 코드입니다.
K.set_learning_phase(1)
K.set_image_data_format('channels_last')
tb_callback = keras.callbacks.TensorBoard(
log_dir=log_path,
histogram_freq=2,
write_graph=True
)
tb_callback.set_model(model)
callbacks = []
callbacks.append(tb_callback)
# Train net:
history = model.fit(
[x_train],
[y_train, y_train_c],
batch_size=int(hype_space['batch_size']),
epochs=EPOCHS,
shuffle=True,
verbose=1,
callbacks=callbacks,
validation_data=([x_test], [y_test, y_test_coarse])
).history
# Test net:
K.set_learning_phase(0)
score = model.evaluate([x_test], [y_test, y_test_coarse], verbose=0)
기본적 histogram_freq=2
으로이 콜백을 호출 할 때 조정해야 할 가장 중요한 매개 변수는 디스크에서 더 적은 파일을 생성하는 것을 목표로 콜백을 호출 할 에포크 간격을 설정합니다.
다음은 "히스토그램"탭 아래 TensorBoard에서 본 훈련 전체의 마지막 컨볼 루션에 대한 값의 진화에 대한 시각화 예제입니다.
In case you would like to see a full example in context, you can refer to this open-source project: https://github.com/Vooban/Hyperopt-Keras-CNN-CIFAR-100
You wrote log_dir='/Graph'
did you mean ./Graph
instead? You sent it to /home/user/Graph
at the moment.
You should check out Losswise (https://losswise.com), it has a plugin for Keras that's easier to use than Tensorboard and has some nice extra features. With Losswise you'd just use from losswise.libs import LosswiseKerasCallback
and then callback = LosswiseKerasCallback(tag='my fancy convnet 1')
and you're good to go (see https://docs.losswise.com/#keras-plugin).
There are few things.
First, not /Graph
but ./Graph
Second, when you use the TensorBoard callback, always pass validation data, because without it, it wouldn't start.
Third, if you want to use anything except scalar summaries, then you should only use the fit
method because fit_generator
will not work. Or you can rewrite the callback to work with fit_generator
.
To add callbacks, just add it to model.fit(..., callbacks=your_list_of_callbacks)
If you are using google-colab simple visualization of the graph would be :
import tensorboardcolab as tb
tbc = tb.TensorBoardColab()
tensorboard = tb.TensorBoardColabCallback(tbc)
history = model.fit(x_train,# Features
y_train, # Target vector
batch_size=batch_size, # Number of observations per batch
epochs=epochs, # Number of epochs
callbacks=[early_stopping, tensorboard], # Early stopping
verbose=1, # Print description after each epoch
validation_split=0.2, #used for validation set every each epoch
validation_data=(x_test, y_test)) # Test data-set to evaluate the model in the end of training
Create the Tensorboard callback:
from keras.callbacks import TensorBoard
from datetime import datetime
logDir = "./Graph/" + datetime.now().strftime("%Y%m%d-%H%M%S") + "/"
tb = TensorBoard(log_dir=logDir, histogram_freq=2, write_graph=True, write_images=True, write_grads=True)
Pass the Tensorboard callback to the fit call:
history = model.fit(X_train, y_train, epochs=200, callbacks=[tb])
When running the model, if you get a Keras error of
"You must feed a value for placeholder tensor"
try reseting the Keras session before the model creation by doing:
import keras.backend as K
K.clear_session()
참고URL : https://stackoverflow.com/questions/42112260/how-do-i-use-the-tensorboard-callback-of-keras
'Programing' 카테고리의 다른 글
UIBarButtonItem 이미지는 얼마나 커야합니까? (0) | 2020.07.07 |
---|---|
ReferenceError : describe가 정의되지 않은 NodeJ (0) | 2020.07.07 |
함수 호출 전의 @ 문자 (0) | 2020.07.07 |
함수가 SQL 데이터베이스에 존재하는지 확인하는 방법 (0) | 2020.07.07 |
Eclipse에서 소스를 쉽게 첨부 할 수있는 방법이 있습니까? (0) | 2020.07.07 |