Richard
Richard

Reputation: 11

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

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))

I tried several different versions of the combination, but still reported an error about AttributeError: module 'tensorflow_core.compat.v2' has no attribute 'internal'

Upvotes: 1

Views: 1823

Answers (2)

Sergio Díaz
Sergio Díaz

Reputation: 89

Error:

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

Solution:

Install Libraries

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

Import

from tensorflow.keras.models import load_model

Upvotes: 0

user11530462
user11530462

Reputation:

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

Generally you will get above error due to incompatibility between Tensorflow and Keras. You can import keras without any issues by upgrade to latest version. For more details you can refer this solution.

Coming to your code, have couple of issues and it can be resolved

1.to_categorical has now packed in np_utils. You need to add import as shown below

from keras.utils.np_utils import to_categorical 

2.Typo mistake, replace metics to metrics in model.compile

Working code as shown below

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))

Output:

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>

You can refer this gist,for the above use case in tensorflow version.

Upvotes: 1

Related Questions