当试图在keras中拟合一个相当简单的卷积+密集层模型时,我似乎无法完成fit()函数。
x训练数据是(批量大小,高度,宽度,通道)格式的形状i_train.shape(10,1070,1230,3)的numpy数组。 y训练数据的形状为m_train.shape(10,1070,1230)(批量,高度,宽度)格式。
nb_filter = 1
nb_row = 3
nb_col = 3
model = Sequential()
model.add(Conv2D(nb_filter, (nb_row, nb_col), padding = 'same',
activation='relu', input_shape=(h,w,3)))
model.add(MaxPooling2D(pool_size=(2,2), padding = 'valid'))
model.add(Flatten())
model.add(Dense(h*w, activation='relu', input_shape= (329025,)))
model.add(BatchNormalization())
model.add(Reshape((h,w)))
model.compile(loss='mean_squared_error', optimizer='SGD', metrics=['accuracy'])
model.fit(i_train, m_train, batch_size=10, epochs=10, verbose=1)
我得到的错误是
Traceback (most recent call last):
File "<ipython-input-9-639284a1176d>", line 1, in <module>
model.fit(i_train, m_train, batch_size=10, epochs=10, verbose=1)
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\keras\models.py", line 870, in fit
initial_epoch=initial_epoch)
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\keras\engine\training.py", line 1490, in fit
self._make_train_function()
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\keras\engine\training.py", line 1014, in _make_train_function
self.total_loss)
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\keras\optimizers.py", line 162, in get_updates
moments = [K.zeros(shape) for shape in shapes]
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\keras\optimizers.py", line 162, in <listcomp>
moments = [K.zeros(shape) for shape in shapes]
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\keras\backend\tensorflow_backend.py", line 601, in zeros
return variable(tf.constant_initializer(0., dtype=tf_dtype)(shape),
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\tensorflow\python\ops\init_ops.py", line 162, in __call__
verify_shape=self.verify_shape)
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\tensorflow\python\framework\constant_op.py", line 102, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "C:\Users\Jacob.Rose\AppData\Local\Continuum\Anaconda2\envs\TFlow\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 424, in make_tensor_proto
(shape_size, nparray.size))
ValueError: Too many elements provided. Needed at most -761894396, but received 1