Typeerror unsupported operand type s for nonetype and int
Typeerror unsupported operand type s for nonetype and int
TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’
In python, TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ error occurs when an integer value is added to a variable that is None. You can add an integer number with a another number. You can’t add a number to None. The Error TypeError: unsupported operand type(s) for +: ‘int’ and ‘NoneType’ will be thrown if an integer value is added with an operand or an object that is None.
An mathematical addition is performed between two numbers. It may be an integer, a float, a long number, etc. The number can not be added to the operand or object that is None. None means that no value is assigned to the variable. So if you try to add a number to a variable that is None, the error TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ will be thrown.
Exception
The error TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ will be shown as below the stack trace. The stack trace shows the line where an integer value is added to a variable that is None.
How to reproduce this error
If you try to add an integer value with a variable that is not assigned to a value, the error will be reproduced. Create two python variables. Assign a variable that has a valid integer number. Assign another variable to None. Add the two variables to the arithmetic addition operator.
Output
Root Cause
In Python, the arithmetic operation addition can be used to add two valid numbers. If an integer value is added with a variable that is assigned with None, this error TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ will be thrown. The None variable should be assigned a valid number before it is added to the integer value.
Solution 1
If an integer value is added with a variable that is None, the error will be shown. A valid integer number must be assigned to the unassigned variable before the mathematical addition operation is carried out. This is going to address the error.
Output
Solution 2
If the variable is assigned to None, skip to add the variable to the integer. If the variable does not have a value, you can not add it. If the variable type is NoneType, avoid adding the variable to the integer. Otherwise, the output will be unpredictable. If a variable is None, use an alternate flow in the code.
Output
Solution 3
If the variable is assigned to None, assign the default value to the variable. For eg, if the variable is None, assign the variable to 0. The error will be thrown if the default value is assigned to it. The variable that is None has no meaning at all. You may assign a default value that does not change the value of the expression. In addition, the default value is set to 0. In the case of multiplication, the default value is 1.
TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘int’ #1319
Comments
angelo337 commented Dec 21, 2015
hi there
I am new to eras, and learning about it, for my fist model I am trying create a CNN 1D and every time I try to compile this I am getting the same error I try with even and Odds parameters,
here is the errors and bellow is the source code of my model. could some one please point me out my error and some resource for the solution?
thanks a lot
angelo
Traceback (most recent call last):
File «character_cnn.py», line 206, in
model.add(Dense(hidden_dims))
File «/usr/local/lib/python2.7/dist-packages/keras/layers/containers.py», line 32, in add
self.layers[-1].set_previous(self.layers[-2])
File «/usr/local/lib/python2.7/dist-packages/keras/layers/core.py», line 34, in set_previous
assert self.input_ndim == len(layer.output_shape), «Incompatible shapes: layer expected input with ndim=» +
File «/usr/local/lib/python2.7/dist-packages/keras/layers/core.py», line 588, in output_shape
return (input_shape[0], np.prod(input_shape[1:]))
File «/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py», line 2481, in prod
out=out, keepdims=keepdims)
File «/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py», line 35, in _prod
return umr_prod(a, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘int’
nb_samples = X_train.shape[0]
nb_features = X_train.shape[1]
newshape = (nb_samples, 1, nb_features, 1)
X_train = np.reshape(X_train, newshape).astype(np.int)
We set some hyperparameters
BATCH_SIZE = 16
maxlen = 10
max_features = a.shape[1]
FIELD_SIZE = 5 * 300
STRIDE = 300
N_FILTERS = a.shape[1]
nb_classes =22
embedding_dims = 50
nb_filters = 250
filter_length = 2
number of hidden nodes in full connected layer
print(‘Build model. ‘)
model = Sequential()
model.add(Embedding(max_features, embedding_dims))
model.add(Dropout(0.25))
print «embedding dims: «, embedding_dims
we add a Convolution1D, which will learn nb_filter
word group filters of size filter_length:
model.add(Convolution1D(input_dim=embedding_dims,
nb_filter=nb_filters,
filter_length=filter_length,
border_mode=»valid»,
activation=»relu»,
subsample_length=1))
model.add(Activation(‘relu’))
we use standard max pooling (halving the output of the previous layer):
We flatten the output of the conv layer,
so that we can add a vanilla dense layer:
Computing the output shape of a conv layer can be tricky;
for a good tutorial, see: http://cs231n.github.io/convolutional-networks/
I get the error in here no matter any parameter that I change
model.add(Dense(hidden_dims))
model.add(Dropout(0.25))
model.add(Activation(‘relu’))
We project onto a single unit output layer, and squash it with a sigmoid:
model.compile(loss=’categorical_crossentropy’,
optimizer=’adadelta’)
print «fitting model»
model.fit(X_train, y_train, batch_size=BATCH_SIZE, verbose=1,
nb_epoch=10, show_accuracy=True,
validation_split=0.1)
score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
print(‘Test score:’, score[0])
print(‘Test accuracy:’, score[1])
The text was updated successfully, but these errors were encountered: