2017-06-25 81 views
5

Tôi đã đoạn mã sau chạy bên trong một máy tính xách tay Jupyter:Keras + TensorFlow Realtime biểu đồ đào tạo

# Visualize training history 
from keras.models import Sequential 
from keras.layers import Dense 
import matplotlib.pyplot as plt 
import numpy 
# fix random seed for reproducibility 
seed = 7 
numpy.random.seed(seed) 
# load pima indians dataset 
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",") 
# split into input (X) and output (Y) variables 
X = dataset[:,0:8] 
Y = dataset[:,8] 
# create model 
model = Sequential() 
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu')) 
model.add(Dense(8, kernel_initializer='uniform', activation='relu')) 
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid')) 
# Compile model 
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
# Fit the model 
history = model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10, verbose=0) 
# list all data in history 
print(history.history.keys()) 
# summarize history for accuracy 
plt.plot(history.history['acc']) 
plt.plot(history.history['val_acc']) 
plt.title('model accuracy') 
plt.ylabel('accuracy') 
plt.xlabel('epoch') 
plt.legend(['train', 'test'], loc='upper left') 
plt.show() 
# summarize history for loss 
plt.plot(history.history['loss']) 
plt.plot(history.history['val_loss']) 
plt.title('model loss') 
plt.ylabel('loss') 
plt.xlabel('epoch') 
plt.legend(['train', 'test'], loc='upper left') 
plt.show() 

Mã này thu thập lịch sử thời đại, sau đó hiển thị lịch sử tiến bộ.


Q: Làm thế nào tôi có thể thực hiện thay đổi bảng xếp hạng trong khi đào tạo để tôi có thể thấy những thay đổi trong thời gian thực?

Trả lời

4

Máy ảnh đi kèm với callback for TensorBoard.

Bạn có thể dễ dàng thêm hành vi này vào mô hình của mình và sau đó chỉ cần chạy tensorboard ở phía trên cùng của dữ liệu ghi nhật ký.

callbacks = [TensorBoard(log_dir='./logs')] 
result = model.fit(X, Y, ..., callbacks=callbacks) 

Và sau đó trên vỏ của bạn:

tensorboard --logdir=/logs 

Nếu bạn cần nó trong máy tính xách tay của bạn, bạn cũng có thể viết callback của riêng bạn để có được số liệu trong khi đào tạo:

class LogCallback(Callback): 

    def on_epoch_end(self, epoch, logs=None): 
     print(logs["train_accuracy"]) 

sẽ này có được độ chính xác đào tạo vào cuối kỷ nguyên hiện tại và in nó. There's some good documentation around it on the official keras site.

Các vấn đề liên quan