Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras - TypeError: 'int' object is not iterable

I was trying to test a network, but seem to get an annoying error, which I am not quite sure I understand.

import keras
from keras.models import Sequential
from keras.optimizers import SGD
from keras.layers.core import Dense, Activation, Lambda, Reshape,Flatten
from keras.layers import Conv1D,Conv2D,MaxPooling2D, MaxPooling1D, Reshape
from keras.utils import np_utils
from keras.models import Model
from keras.layers import Input, Dense
from keras.layers import Dropout
from keras import backend as K
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import CSVLogger
from keras.callbacks import EarlyStopping
from keras.layers.merge import Concatenate
from keras.callbacks import ModelCheckpoint
import random
import numpy as np


window_height = 8
filter_size=window_height
pooling_size = 28
stride_step = 2


def fws():


    np.random.seed(100)
    input = Input(5,window_height,1)
    shared_conv = Conv2D(filters = 1, kernel_size = (0,window_height,1))
    output = shared_conv(input)
    print output.shape


fws()

Error message:

File "experiment.py", line 34, in <module>
   fws()
 File "experiment.py", line 29, in fws
   input = Input(5,window_height,1)
 File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 1426, in Input
   input_tensor=tensor)
 File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 87, in wrapper
   return func(*args, **kwargs)
 File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 1321, in __init__
   batch_input_shape = tuple(batch_input_shape)
TypeError: 'int' object is not iterable

Why am i getting this error?

I am in the network trying to use shared convolution layer, which the code states, and for test purposes want to see what the output became?..

like image 410
Lamda Avatar asked Oct 03 '17 10:10

Lamda


People also ask

How do I fix TypeError int object is not iterable?

How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.

How do I fix TypeError type object is not iterable in python?

Conclusion # The Python "TypeError: 'type' object is not iterable" occurs when we try to iterate over a class that is not iterable, e.g. forget to call the range() function. To solve the error, make the class iterable by implementing the __iter__() method.


1 Answers

your line:

input = Input(5,window_height,1)

is giving this error. compare this with an example from keras: https://keras.io/getting-started/functional-api-guide/

inputs = Input(shape=(784,))

the Input object is expecting an iterable for shape but you passed it an int. In the example you can see how they get around that for a 1 dimensional input.

EDIT: I don't know why this is a popular answer - if you're getting this error because you're following bad example code somewhere, be sure to raise that with whatever source you're getting it from.

like image 98
Stael Avatar answered Sep 27 '22 23:09

Stael