matplotlib plt.style.use('./deeplearning. mplstyle')即使在正确下载文件后也无法工作

zengzsys  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(466)

在完成Deeplearning.AI的机器学习专业化之后,我试图在概念和实验室上修改自己,我设法下载了所有文件并将它们存储在同一个文件夹中。这是问题的一个变体(plt.style.use('./deeplearning.mplstyle') is not working)。但我有一个不同的错误。

TypeError: the 'package' argument is required to perform a relative import for './deeplearning'

我试着运行这段代码

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras import Sequential
from tensorflow.keras.losses import MeanSquaredError, BinaryCrossentropy
from tensorflow.keras.activations import sigmoid
from lab_utils_common import dlc

并得到了错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 8
      6 from tensorflow.keras.losses import MeanSquaredError, BinaryCrossentropy
      7 from tensorflow.keras.activations import sigmoid
----> 8 from lab_utils_common import dlc
      9 from lab_neurons_utils import plt_prob_1d, sigmoidnp, plt_linear, plt_logistic
     10 plt.style.use('./deeplearning.mplstyle')

File ~\Desktop\ML2\Week1\Files\Files\home\jovyan\work\lab_utils_common.py:22
     20 dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0'; dldarkblue =  '#0D5BDC'
     21 dlcolors = [dlblue, dlorange, dldarkred, dlmagenta, dlpurple]
---> 22 plt.style.use('./deeplearning.mplstyle')
     24 def sigmoid(z):
     25     """
     26     Compute the sigmoid of z
     27 
   (...)
     36          sigmoid(z)
     37     """

File C:\ProgramData\anaconda3\lib\site-packages\matplotlib\style\core.py:153, in use(style)
    151 pkg, _, name = style.rpartition(".")
    152 try:
--> 153     path = (importlib_resources.files(pkg)
    154             / f"{name}.{STYLE_EXTENSION}")
    155     style = _rc_params_in_file(path)
    156 except (ModuleNotFoundError, IOError) as exc:
    157     # There is an ambiguity whether a dotted name refers to a
    158     # package.style_name or to a dotted file path.  Currently,
   (...)
    161     # either use Path objects or be prepended with "./" and use
    162     # the slash as marker for file paths.

File C:\ProgramData\anaconda3\lib\importlib\_common.py:22, in files(package)
     17 def files(package):
     18     # type: (Package) -> Traversable
     19     """
     20     Get a Traversable resource from a package
     21     """
---> 22     return from_package(get_package(package))

File C:\ProgramData\anaconda3\lib\importlib\_common.py:66, in get_package(package)
     60 def get_package(package):
     61     # type: (Package) -> types.ModuleType
     62     """Take a package name or module object and return the module.
     63 
     64     Raise an exception if the resolved module is not a package.
     65     """
---> 66     resolved = resolve(package)
     67     if wrap_spec(resolved).submodule_search_locations is None:
     68         raise TypeError(f'{package!r} is not a package')

File C:\ProgramData\anaconda3\lib\importlib\_common.py:57, in resolve(cand)
     55 def resolve(cand):
     56     # type: (Package) -> types.ModuleType
---> 57     return cand if isinstance(cand, types.ModuleType) else importlib.import_module(cand)

File C:\ProgramData\anaconda3\lib\importlib\__init__.py:121, in import_module(name, package)
    118 if not package:
    119     msg = ("the 'package' argument is required to perform a relative "
    120            "import for {!r}")
--> 121     raise TypeError(msg.format(name))
    122 for character in name:
    123     if character != '.':

TypeError: the 'package' argument is required to perform a relative import for './deeplearning'

从我所能理解的和我能解码的问题来看,似乎./deeplearning.mplstyle根本没有作为matplotlib的选项进行更新,不幸的是,更改为任何其他样式都会导致课程中其他文件也使用deeplearning. mplstyle产生更多错误。
我应该把包参数放在哪里来解决这个相对重要的问题?

67up9zun

67up9zun1#

我建议更新matplotlib库到最新版本。
出现此错误是因为matplotlib将字符串“./deeplearning.mplstyle”视为包,而不是文件路径。在matplotlib版本>= 3.7.1中,TypeError异常被静默处理。

相关问题