【问题标题】:AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'AttributeError:模块“tensorflow_core.compat.v2”没有属性“__internal__”
【发布时间】:2021-07-14 05:06:44
【问题描述】:
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import matplotlib.pyplot as plt

(x_train, y_train), (x_test, y_test) = mnist.load_data()

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

im = plt.imshow(x_train[0], cmap="gray")
plt.show()


x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)

x_train = x_train/255
x_test = x_test/255

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

model.compile(optimizer=SGD(), loss='categorical_crossentropy', metics=['accuracy'])
model.fit(x_train, y_train, batch_size=64, epochs=5, validation_data=(x_test, y_test))

我尝试了几种不同版本的组合,但仍然报错关于 AttributeError:模块“tensorflow_core.compat.v2”没有属性“internal

【问题讨论】:

  • 我也遇到了同样的问题,不知道怎么解决

标签: tensorflow


【解决方案1】:
AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'

由于TensorflowKeras 之间的不兼容,通常会出现上述错误。通过升级到最新版本,您可以毫无问题地导入keras。更多详情可以参考solution

来到你的代码,有几个问题,可以解决

1.to_categorical 现在已打包到np_utils。您需要添加导入,如下所示

from keras.utils.np_utils import to_categorical 

2.Typo错误,将model.compile中的metics替换为metrics

工作代码如下图

import keras
print(keras.__version__)
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import matplotlib.pyplot as plt
from keras.utils.np_utils import to_categorical


(x_train, y_train), (x_test, y_test) = mnist.load_data()

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

im = plt.imshow(x_train[0], cmap="gray")
plt.show()


x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)

x_train = x_train/255
x_test = x_test/255

y_train = to_categorical(y_train, 10) 
y_test = to_categorical(y_test, 10)   

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

model.compile(optimizer=SGD(), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=64, epochs=5, validation_data=(x_test, y_test))

输出:

2.5.0
(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 512)               401920    
_________________________________________________________________
dense_1 (Dense)              (None, 256)               131328    
_________________________________________________________________
dense_2 (Dense)              (None, 10)                2570      
=================================================================
Total params: 535,818
Trainable params: 535,818
Non-trainable params: 0
_________________________________________________________________
Epoch 1/5
938/938 [==============================] - 21s 8ms/step - loss: 1.2351 - accuracy: 0.6966 - val_loss: 0.3644 - val_accuracy: 0.9011
Epoch 2/5
938/938 [==============================] - 7s 7ms/step - loss: 0.3554 - accuracy: 0.9023 - val_loss: 0.2943 - val_accuracy: 0.9166
Epoch 3/5
938/938 [==============================] - 7s 7ms/step - loss: 0.2929 - accuracy: 0.9176 - val_loss: 0.2553 - val_accuracy: 0.9282
Epoch 4/5
938/938 [==============================] - 7s 7ms/step - loss: 0.2538 - accuracy: 0.9281 - val_loss: 0.2309 - val_accuracy: 0.9337
Epoch 5/5
938/938 [==============================] - 7s 8ms/step - loss: 0.2313 - accuracy: 0.9355 - val_loss: 0.2096 - val_accuracy: 0.9401
<keras.callbacks.History at 0x7f615c82d090>

你可以参考这个gist,用于tensorflow版本的上述用例。

【讨论】:

    【解决方案2】:

    错误:

    AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'
    

    解决方案:

    安装库

    !pip install tensorflow==2.1
    !pip install keras==2.3.1
    

    导入

    from tensorflow.keras.models import load_model
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 2021-08-14
      • 1970-01-01
      • 2018-04-14
      相关资源
      最近更新 更多