I was trying to run the following code for creating a network in keras:
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
num_classes=len(np.unique(y_train))
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
but I receive the following error message
TypeError: 'NoneType' object is not iterable
here it is the full error traceback:
Traceback (most recent call last):
File ".../filename.py", line 31, in <module>
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
File "...\Python38\lib\site-packages\keras\engine\sequential.py", line 166, in add
layer(x)
File "...\Python38\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
return func(*args, **kwargs)
File "...\Python38\lib\site-packages\keras\engine\base_layer.py", line 463, in __call__
self.build(unpack_singleton(input_shapes))
File "...\Python38\lib\site-packages\keras\layers\convolutional.py", line 137, in build
self.kernel = self.add_weight(shape=kernel_shape,
File "...\Python38\lib\site-packages\keras\engine\base_layer.py", line 279, in add_weight
weight = K.variable(initializer(shape, dtype=dtype),
File "...\Python38\lib\site-packages\keras\initializers.py", line 226, in __call__
x = K.random_uniform(shape, -limit, limit,
File "...\Python38\lib\site-packages\keras\backend\tensorflow_backend.py", line 4356, in random_uniform
return tf_keras_backend.random_uniform(
File "...\Python38\lib\site-packages\tensorflow\python\keras\backend.py", line 5685, in random_uniform
return random_ops.random_uniform(
File "...\Python38\lib\site-packages\tensorflow\python\ops\random_ops.py", line 282, in random_uniform
shape = tensor_util.shape_tensor(shape)
File "...\Python38\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 1015, in shape_tensor
return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
File "...\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 1341, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "...\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 321, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "...\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 261, in constant
return _constant_impl(value, dtype, shape, name, verify_shape=False,
File "...\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 270, in _constant_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "...\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 95, in convert_to_eager_tensor
ctx.ensure_initialized()
File "...\Python38\lib\site-packages\tensorflow\python\eager\context.py", line 502, in ensure_initialized
config_str = self.config.SerializeToString()
File "...\Python38\lib\site-packages\tensorflow\python\eager\context.py", line 880, in config
self._initialize_physical_devices()
File "...\Python38\lib\site-packages\tensorflow\python\eager\context.py", line 1167, in _initialize_physical_devices
self._physical_devices = [
For what I can understand, looking at the Tracebabk, the problem is in the file context.py
at line 1166:
devs = pywrap_tfe.TF_ListPhysicalDevices()
devs is <class 'NoneType'> then at the following line
self._physical_devices = [PhysicalDevice(name=d.decode(),device_type=d.decode().split(":")[1]) for d in devs]
when it tries to iterate on it, it obviously raises the TypeError: 'NoneType' object is not iterable.
For some reason the list of physical devices is empty, but the function TF_ListPhysicalDevices()
does not yield and empty list but a None.
Anyway even if I set self._physical_devices
to an empty list, it will then raise another error elsewhere (tensorflow.python.framework.errors_impl.NotFoundError: No CPU devices are available in this process
)
Any idea on how I can solve this. Thank you.
I have copied the code I was trying to run from the webpage https://data-flair.training/blogs/python-deep-learning-project-handwritten-digit-recognition