python - Keras Tensorflow reshape error during epoch
问题描述
I am new to Keras and Tensorflow and I am trying to use the LSTM to train on some radar pulse dataset I have. During compilation I have made it work, but when the model starts to train I cannot get it working. I don't know where to place the reshape and which dimensions to use since the numbers that the error gives I don't know where they come from.
So the code is:
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)
And the error tells:
> 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
模型输入的维度引入动态大小(参见None
in input_shape=(None, x_train.shape[1], 1)
)。因为None
仅在推理时将被实际尺寸大小替换,Keras - 在编译时 - 相信您提供具有0
适当尺寸尺寸的样本,以便它们的元素数量在Reshape
equals之前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))
尝试运行此代码时,您将在 处出现“异常” ValueError: total size of new array must be unchanged
,model.add(Reshape((3, 4)))
因为 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
,因此出现错误。
推荐阅读
- javascript - 如何在材质表中使用自定义“editComponent”?
- reactjs - 如何克隆反应状态数组而不对其进行更改?
- mysql - 如何提高 200+ 百万条记录的查询性能
- javers - 当实体字段类型更改(继承)时,Javers 返回不完整的差异
- node.js - 无法导出功能
- python - 将嵌套列表/数组保存到 CSV 以便以后轻松加载的最佳方式
- swift - SwiftUI - 列表中的两个按钮
- python - 请求未能解码响应
- mongodb - 是否可以在 Stitch Custom Authentication/Stitch Auth 中使用 Firebase signInWithEmailAndPassword
- angular - 尽管服务器状态显示为在线,但在我的角度解耦项目中获取 502 Bad Gateway