【发布时间】:2020-11-01 00:31:42
【问题描述】:
我有一个名为utils.py 的文件。在该文件中,我有一个名为 plot_results 的函数,定义如下:
def plot_results(results, epochs):
"""
The function to show results on each epoch.
Parameters:
results (keras.history): History of each epoch. It comes directly from keras.
epochs (int): The number of epochs.
"""
_, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_xlabel("Epochs")
ax1.set_ylabel("Losses")
ax1.plot(
range(1, epochs+1),
results.history['val_loss'],
label="Validation loss",
marker='o')
ax1.plot(
range(1, epochs+1),
results.history['loss'],
label="loss",
marker='o')
ax1.legend()
ax2.set_xlabel("Epochs")
ax2.set_ylabel("Accuracies")
ax2.plot(
range(1, epochs+1),
[accuracy * 100 for accuracy in results.history['accuracy']],
label="Accuracy",
marker='o')
ax2.plot(
range(1, epochs+1),
[accuracy * 100 for accuracy in results.history['val_accuracy']],
label="validation accuracy",
marker='o')
ax2.legend()
plt.show()
我还有一个名为main.py 的文件,在该文件中我称之为plot_results。当我在本地机器上运行 main.py 时,我正确地得到了可视化的情节。
但是当我在 google colab 单元格中运行它时:
! python main.py --ne 1
我刚收到<Figure size 640x480 with 2 Axes>
根据this post我试过了:
%matplotlib inline
! python main.py --ne 1
还有:
%matplotlib notebook
! python main.py --ne 1
还有:
%matplotlib inline
%matplotlib notebook
! python main.py --ne 1
但它们都不起作用。 如何在该函数中显示绘图?
【问题讨论】:
标签: python matplotlib ipython google-colaboratory