AttributeError … has no attribute ‘_TensorLike’

久しぶりにKerasを使ったら落ちたのでメモ。

TL;DR

from keras import ... ではなく from tensorflow.keras import ...

発生するエラー

AttributeError: module ‘tensorflow.python.framework.ops’ has no attribute ‘_TensorLike’

問題のコード

from keras import Sequential
from keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense
import tensorflow as tf
model = Sequential()
# ここで落ちる
model.add(Conv2D(64,(3,3), padding='same', input_shape=(192,192,3))
opt = tf.keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
# ...

修正

# from keras import Sequential
from tensorflow.keras.models import Sequential
# from keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense
import tensorflow as tf
model = Sequential()
model.add(Conv2D(64,(3,3), padding='same', input_shape=(192,192,3))
# opt = tf.keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
opt = tf.keras.optimizers.RMSprop(lr=0.0001, decay=1e-6)
# ...