Keras Tensorflow 在纪元期间重塑错误

Keras Tensorflow reshape error during epoch

我是 Keras 和 Tensorflow 的新手,我正在尝试使用 LSTM 在我拥有的一些雷达脉冲数据集上进行训练。 在编译期间我已经让它工作了,但是当模型开始训练时我无法让它工作。我不知道在哪里放置整形以及使用哪些尺寸,因为错误给出的数字我不知道它们来自哪里。

所以代码是:

x_train = load_data("D:\Software_Herramienta\Datasets\sweep_switch_train.csv")
y_train = load_data("D:\Software_Herramienta\Datasets\sweep_switch_labels_train.csv")
x_train = x_train.reshape(-1, x_train.shape[0], x_train.shape[1], 1)
y_train.astype(int)
y_train = y_train.reshape(1,1000)

batch = 10

model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(None, x_train.shape[1], 1)))
model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(Dense(32, name="first_dense")))
model.add(Reshape((3, 4)))
model.add(LSTM(x_train.shape[1], dropout_U=0.2, dropout_W=0.2))
# model.add(Dense(1, activation='softmax'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=15, batch_size=batch)

错误告诉我们:

> Epoch 1/15
2018-06-13 13:40:17.225066: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\client\session.py", line 1322, in _do_call
    return fn(*args)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\client\session.py", line 1307, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\client\session.py", line 1409, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 31872128 values, but the requested shape has 12
     [[Node: reshape_1/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](time_distributed_2/add, reshape_1/Reshape/shape)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "D:/Software_Herramienta/Pulse_Generator/pulse_model_keras_tf.py", line 74, in <module>
    model.fit(x_train, y_train, epochs=15, batch_size=batch)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\keras\models.py", line 1002, in fit
    validation_steps=validation_steps)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\keras\engine\training.py", line 1705, in fit
    validation_steps=validation_steps)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\keras\engine\training.py", line 1236, in _fit_loop
    outs = f(ins_batch)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\keras\backend\tensorflow_backend.py", line 2482, in __call__
    **self.session_kwargs)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
    run_metadata_ptr)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\client\session.py", line 1135, in _run
    feed_dict_tensor, options, run_metadata)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\client\session.py", line 1316, in _do_run
    run_metadata)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\client\session.py", line 1335, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 31872128 values, but the requested shape has 12
     [[Node: reshape_1/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](time_distributed_2/add, reshape_1/Reshape/shape)]]
Caused by op 'reshape_1/Reshape', defined at:
  File "D:/Software_Herramienta/Pulse_Generator/pulse_model_keras_tf.py", line 69, in <module>
    model.add(Reshape((3, 4)))
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\keras\models.py", line 522, in add
    output_tensor = layer(self.outputs[0])
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\keras\engine\topology.py", line 619, in __call__
    output = self.call(inputs, **kwargs)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\keras\layers\core.py", line 406, in call
    return K.reshape(inputs, (K.shape(inputs)[0],) + self.target_shape)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\keras\backend\tensorflow_backend.py", line 1898, in reshape
    return tf.reshape(x, shape)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 7323, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\framework\ops.py", line 3392, in create_op
    op_def=op_def)
  File "D:\Software_Utils\Python_3.6.5\lib\site-packages\tensorflow\python\framework\ops.py", line 1718, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 31872128 values, but the requested shape has 12
     [[Node: reshape_1/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](time_distributed_2/add, reshape_1/Reshape/shape)]]

作为输入给 Reshape 层的元素数必须等于其输出中的元素数(output_shape=(None, 3, 4),所以 batch_size * 12 个元素),这不是目前是这样。

您的模型仍在编译,因为您为模型输入的维度 0 引入了动态大小(c.f。input_shape=(None, x_train.shape[1], 1) 中的 None)。因为 None 只会在推理时被实际维度大小替换,Keras - 在编译时 - 相信您可以使用适当大小的维度 0 来提供样本,以便它们在Reshape 等于 3 * 4 = 12。由于您最终使用 model.fit() 提供的元素最终变得更大,因此推理在 Reshape.

处失败

具体来说,让我们获取您的代码并对其进行分析。根据您跟踪中的值,我假设您的 sweep_switch_train.csv 包含每个形状 1000x1000x1 的样本:

# Mocking 50 samples of size 1000x1000x1 and their labels:
x_train = np.random.rand(50, 1000, 1000, 1)
y_train = np.random.randint(2, size=50)

让我们尝试构建您的模型:

model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=x_train.shape[1:]))
# notice we set input_shape to (1000, 1000, 1) instead of (None, 1000, 1) the way you were.
# This makes the example clearer as Keras directly knows all dimensions.
model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(Dense(32, name="first_dense")))
model.add(Reshape((3, 4)))
model.add(LSTM(x_train.shape[1], dropout_U=0.2, dropout_W=0.2))

尝试 运行 此代码,您将在 model.add(Reshape((3, 4))) 处出现异常“ValueError: total size of new array must be unchanged”,因为 Keras 会直接注意到元素数量与重塑不匹配(因为这里不再有 None 维度大小引起的歧义)。

如果我们逐行查看您的层的输出形状,直到出现异常:

model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=x_train.shape[1:]))
print(model.layers[0].output_shape)
# (None, 998, 998, 32)
model.add(TimeDistributed(Activation('relu')))
print(model.layers[1].output_shape)
# (None, 998, 998, 32)
model.add(TimeDistributed(Dense(32, name="first_dense")))
print(model.layers[2].output_shape)
# (None, 998, 998, 32)
model.add(Reshape((3, 4)))

如您所见,您正试图将 998 * 998 * 32 = 31872128 个元素的矩阵(跟踪中的值)重塑为 3 * 4 = 12 个元素的矩阵,因此出现错误。