在 colab 中使用 运行 py 文件时,使用 %matplotlib notebook 或 %matplotlib notebook 不起作用
Using %matplotlib notebook or %matplotlib notebook does not work when run py file in colab
我有一个名为 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>
根据 我试过:
%matplotlib inline
! python main.py --ne 1
并且:
%matplotlib notebook
! python main.py --ne 1
并且:
%matplotlib inline
%matplotlib notebook
! python main.py --ne 1
但其中 none 有效。
如何在该函数中显示绘图?
试试这个
%run main.py
它将像 运行 一样逐行工作。
我有一个名为 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>
根据
%matplotlib inline
! python main.py --ne 1
并且:
%matplotlib notebook
! python main.py --ne 1
并且:
%matplotlib inline
%matplotlib notebook
! python main.py --ne 1
但其中 none 有效。 如何在该函数中显示绘图?
试试这个
%run main.py
它将像 运行 一样逐行工作。