Keras Tensorflow在纪元期间重塑错误

时间:2018-06-13 11:45:15

标签: python tensorflow keras reshape

我是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)]]

1 个答案:

答案 0 :(得分:1)

作为Reshape图层输入的元素数量必须等于其输出中的元素数量(output_shape=(None, 3, 4),所以batch_size * 12元素),目前不是情况下。

您的模型仍在编译中,因为您为模型输入的维0引入了动态尺寸(例如None中的input_shape=(None, x_train.shape[1], 1)。因为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))

尝试运行此代码,您将遇到异常&#34; ValueError: total size of new array must be unchanged&#34;在model.add(Reshape((3, 4))),因为Keras会直接注意到重塑的元素数量不匹配(因为此处None尺寸大小不会导致模糊不清。)

如果我们逐行查看您的图层&#39;输出形状直到例外:

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元素的矩阵,从而导致错误。