0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

直感 Deep Learningのソースコード置き換え

0
Posted at

はじめに

2018年に出版された直感Deep Leaningを2023年現在写経しているとエラーになる箇所があったため記録として残しておく。

環境

Python: 3.10.9
gensim: 4.3.0
h5py: 3.8.0
keras: 2.11.0
matplotlib: 3.6.3
nltk: 3.8.1
numpy: 1.24.1
Pillow: 9.4.0
scikit-learn: 1.2.1
tensorboard: 2.11.2
tensorflow: 2.11.0

置き換え

pad_sequencesをimport、または使おうとした時にエラーになる。
旧

from keras.preprocessing import sequence
.
.
hoge = sequence.pad_sequences(...)

新

from tensorflow.keras.utils import pad_sequences
.
.
hoge = pad_sequences(...)

または
旧

from keras.preprocessing.sequence import pad_sequences

新

from tensorflow.keras.utils import pad_sequences

旧

from keras.preprocessing.image import load_img

新

from tensorflow.keras.utils import load_img

旧

from keras.preprocessing.image import img_to_array

新

from tensorflow.keras.utils import img_to_array

モジュールのimportでエラーになる。
(別の章では新のほうのimportをしているので一貫性がないように感じる)
旧

from keras.layers.embeddings import Embedding

新

from keras.layers import Embedding

旧

from keras.layers.recurrent import LSTM

新

from keras.layers import LSTM

旧

from keras.engine.topology import Layer

新

from keras.layers import Layer

Kerasのgradientsが非推奨になってエラーを出すようになっている。
TensorflowのGradientTapeを使えという内容も出力されるがとりあえず通して動きを見たいということであれば以下を入れることで動かすことは可能。

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

TensorflowのVariableが+=をサポートしていないというエラーが出てきたのでその際は正攻法で修正した。
旧

loss += hoge

新

loss = loss + hoge

終わりに

本書で一部写経していない章もあるので実際には他にもエラーになる箇所があるかもしれない。

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?