Issue retreiving 'int' object is not iterable error in KERAS

Issue

I am trying to run the following code. The following code runs fine on google colab, however on my system it throws an error. Tensorflow version installed on my system is is 1.12.0 and keras version is 2.2.4. Help is highly appreciated.

def profiler(layer, test_input):
 data_input = test_input
 start = time.time()
 data_input = layer.predict(data_input)
 end = time.time() - start
 milliseconds = end * 1000
 return milliseconds 

def dense_layer(input_dim, dense_size):
    x = tf.keras.layers.Input((input_dim))
    dense = tf.keras.layers.Dense(dense_size)(x)
    model = tf.keras.models.Model(inputs=x, outputs=dense)
    return model

def process_config(config):
    tokens = config.split(",")
    values = []
    for token in tokens:
        token = token.strip()
        if token.find("-") == -1:
            token = int(token)
            values.append(token)
        else:
            start,end = token.split("-")
            start = int(start.strip())
            end = int(end.strip())
            values = values + list(range(start,end+1))
    return values

def evaluate_dense(input_shapes_range, dense_size_range):
    for input_shape in input_shapes_range:
        for dense_size in dense_size_range:
            to_write = open("dense_data.csv", "a+")
            model = dense_layer(input_shape, dense_size)
            random_input = np.random.randn(1, input_shape)
            running_time = profiler(model, random_input)
            del model

input_size = "2000"
dense_size = "1000, 4096"
input_size_range = process_config(input_size)
dense_size_range = process_config(dense_size)
evaluate_dense(input_size_range, dense_size_range)

Error trace

  File "C:/Users/Dense-layer.py", line 59, in <module>
    evaluate_dense(input_size_range, dense_size_range)
  File "C:/Users/Dense-layer.py", line 44, in evaluate_dense
    model = dense_layer(input_shape, dense_size)
  File "C:/Users/Dense-layer.py", line 16, in dense_layer
    x = tf.keras.layers.Input((input_dim))
  File "C:\Users\learn\miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 229, in Input
    input_tensor=tensor)
  File "C:\Users\learn\miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 91, in __init__
    batch_input_shape = (batch_size,) + tuple(input_shape)
TypeError: 'int' object is not iterable

Solution

input_shape should be a tuple, but input_dim is an integer. You have passed input_dim, and since you have not specified it by name, it considers it as input_shape. So, just specify it by name:

tf.keras.layers.Input(input_dim=input_dim)

Or if you want to specify the shape, use it like:

tf.keras.layers.Input((input_dim,))

Answered By – Kaveh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published