1. 程式人生 > >python3執行《21個專案玩轉深度學習》遇到的問題

python3執行《21個專案玩轉深度學習》遇到的問題

由於我的環境是python3,而這本書用的是python2,所以學習過程中會有一些程式碼不相容的問題,在此列出我遇到的。

chapter_8中使用自己的資料集進行訓練時會遇到:

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    flags.DEFINE_integer("train_size", np.inf, "The size of train images [np.inf]")
  File "/home/yangguang/machineLearning/venv4ML/lib/python3.6/site-packages/tensorflow/python/platform/flags.py", line 58, in wrapper
    return original_function(*args, **kwargs)
  File "/home/yangguang/machineLearning/venv4ML/lib/python3.6/site-packages/absl/flags/_defines.py", line 315, in DEFINE_integer
    DEFINE(parser, name, default, help, flag_values, serializer, **args)
  File "/home/yangguang/machineLearning/venv4ML/lib/python3.6/site-packages/absl/flags/_defines.py", line 81, in DEFINE
    DEFINE_flag(_flag.Flag(parser, serializer, name, default, help, **args),
  File "/home/yangguang/machineLearning/venv4ML/lib/python3.6/site-packages/absl/flags/_flag.py", line 107, in __init__
    self._set_default(default)
  File "/home/yangguang/machineLearning/venv4ML/lib/python3.6/site-packages/absl/flags/_flag.py", line 196, in _set_default
    self.default = self._parse(value)
  File "/home/yangguang/machineLearning/venv4ML/lib/python3.6/site-packages/absl/flags/_flag.py", line 169, in _parse
    'flag --%s=%s: %s' % (self.name, argument, e))
absl.flags._exceptions.IllegalFlagValueError: flag --train_size=inf: Expect argument to be a string or int, found <class 'float'>

原因是np.inf返回的是float,然而在chapter_8的main.py中,"train_size"定義時使用的是DEFINE_integer, 所以將DEFINE_integer改成DEFINE_float即可.

 

chapter_12中執行python sample.py  時候遇到

  File "sample.py", line 19, in main
    FLAGS.start_string = FLAGS.start_string.decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'

這是因為在python3中str預設是unicode型別,不需要再decode了(decode('utf-8'):把utf-8解碼成unicode編碼)。所以這裡直接將sample.py裡的第19行刪除即可!