如何修复“ +:'NoneType'和'int'的不支持的操作数类型”错误

时间:2019-01-26 18:09:14

标签: keras python-3.6 lstm

当我运行此功能时:

def run_lstm(x_train,x_test,y_train,y_test):
    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.LSTM(500))
#   model.add(tf.keras.layers.Dropout(0.5))
    model.add(tf.keras.layers.Dense(18, activation='sigmoid'))

    model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

    model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test,y_test))

    score = model.evaluate(x_test, y_test)

    print('Test loss:', score[0])
    print('Test accuracy:', score[1])

我收到此错误:

Traceback (most recent call last):
  File "/Users/khaled/Documents/GitHub/deep-learning-for-human-activity-recognition-using-skeleton-datasets/train.py", line 294, in <module>
    run_lstm(x_train, x_test, y_train, y_test)
  File "/Users/khaled/Documents/GitHub/deep-learning-for-human-activity-recognition-using-skeleton-datasets/train.py", line 277, in run_lstm
    model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test,y_test))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1509, in fit
    validation_split=validation_split)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 993, in _standardize_user_data
    class_weight, batch_size)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1029, in _standardize_weights
    self._set_inputs(x)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 426, in _method_wrapper
    method(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1222, in _set_inputs
    self.build(input_shape=input_shape)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 222, in build
    layer.build(shape)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/utils/tf_utils.py", line 149, in wrapper
    output_shape = fn(instance, input_shape)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/layers/recurrent.py", line 552, in build
    self.cell.build(step_input_shape)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/utils/tf_utils.py", line 149, in wrapper
    output_shape = fn(instance, input_shape)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/layers/recurrent.py", line 1934, in build
    constraint=self.kernel_constraint)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 586, in add_weight
    aggregation=aggregation)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 591, in _add_variable_with_custom_getter
    **kwargs_for_getter)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1986, in make_variable
    aggregation=aggregation)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 145, in __call__
    return cls._variable_call(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 141, in _variable_call
    aggregation=aggregation)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 120, in <lambda>
    previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2434, in default_variable_creator
    import_scope=import_scope)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 147, in __call__
    return super(VariableMetaclass, cls).__call__(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 297, in __init__
    constraint=constraint)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 411, in _init_from_args
    initial_value(), name="initial_value", dtype=dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1970, in <lambda>
    shape, dtype=dtype, partition_info=partition_info)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py", line 470, in __call__
    scale /= max(1., (fan_in + fan_out) / 2.)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

我应该改变什么?

谢谢。

1 个答案:

答案 0 :(得分:1)

我通过在代码中添加两行来解决了这个问题:

x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)

函数将是:

def run_lstm(x_train,x_test,y_train,y_test):
     x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
     x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)

     model = tf.keras.models.Sequential()

     model.add(tf.keras.layers.LSTM(50))
#    model.add(tf.keras.layers.Dropout(0.5))
#    model.add(tf.keras.layers.Flatten())

     model.add(tf.keras.layers.Dense(18, activation='sigmoid'))

     model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

     model.fit(x_train, y_train, batch_size=300, epochs=2, validation_data=(x_test,y_test))

     score = model.evaluate(x_test, y_test)

     print('Test loss:', score[0])
     print('Test accuracy:', score[1])