不能更改批次大小以使用keras模型进行训练

时间:2019-07-13 00:18:37

标签: tensorflow keras training-data

我正在使用Tensorflow 2 keras,并且当我尝试通过运行来拟合模型时:

model.fit(x=[example_input_batch, example_target_batch], y=np.array(offset_one_timestep), epochs=10, batch_size=1, use_multiprocessing=True)

很好。但是,我只能使用batch_size的1。任何其他值都将返回错误:


    ---------------------------------------------------------------------------
    InvalidArgumentError                      Traceback (most recent call last)
    <ipython-input-10-8c178df9f9b3> in <module>
         16 
         17 #x is input data and y is the target data
    ---> 18 model.fit(x=[example_input_batch, example_target_batch], y=np.array(offset_one_timestep), epochs=10, batch_size=2, use_multiprocessing=True)
         19 # model.fit(x=[trainQ, trainA], y=np.array(offset_one_timestep), epochs=3, batch_size=1)

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
        641         max_queue_size=max_queue_size,
        642         workers=workers,
    --> 643         use_multiprocessing=use_multiprocessing)
        644 
        645   def evaluate(self,

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_arrays.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
        662         validation_steps=validation_steps,
        663         validation_freq=validation_freq,
    --> 664         steps_name='steps_per_epoch')
        665 
        666   def evaluate(self,

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
        381 
        382         # Get outputs.
    --> 383         batch_outs = f(ins_batch)
        384         if not isinstance(batch_outs, list):
        385           batch_outs = [batch_outs]

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
       3508         value = math_ops.cast(value, tensor.dtype)
       3509       converted_inputs.append(value)
    -> 3510     outputs = self._graph_fn(*converted_inputs)
       3511 
       3512     # EagerTensor.numpy() will often make a copy to ensure memory safety.

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs)
        570       raise TypeError("Keyword arguments {} unknown. Expected {}.".format(
        571           list(kwargs.keys()), list(self._arg_keywords)))
    --> 572     return self._call_flat(args)
        573 
        574   def _filtered_call(self, args, kwargs):

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _call_flat(self, args)
        669     # Only need to override the gradient in graph mode and when we have outputs.
        670     if context.executing_eagerly() or not self.outputs:
    --> 671       outputs = self._inference_function.call(ctx, args)
        672     else:
        673       self._register_gradient()

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/function.py in call(self, ctx, args)
        443             attrs=("executor_type", executor_type,
        444                    "config_proto", config),
    --> 445             ctx=ctx)
        446       # Replace empty list with None
        447       outputs = outputs or None

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
         65     else:
         66       message = e.message
    ---> 67     six.raise_from(core._status_to_exception(e.code, message), None)
         68   except TypeError as e:
         69     if any(ops._is_keras_symbolic_tensor(x) for x in inputs):

    ~/anaconda3/envs/tf2/lib/python3.7/site-packages/six.py in raise_from(value, from_value)

    InvalidArgumentError:  Incompatible shapes: [2,20] vs. [2,20,20]
         [[node loss_3/dense_loss/mul (defined at <ipython-input-10-8c178df9f9b3>:18) ]] [Op:__inference_keras_scratch_graph_7008]

    Function call stack:
    keras_scratch_graph

有人知道这是为什么吗?即使只是删除批处理大小值,也将返回相同的错误。

我的模型是一个非常基本的RNN,带有编码器和解码器。

编辑: 这是我定义的模型:


    encoder_input = tf.keras.layers.Input(shape=(20,))
    encoder_embedding = tf.keras.layers.Embedding(input_dim=vocab_size, output_dim = 64)
    encoder_embedded = encoder_embedding(encoder_input)

    output, state_h, state_c = tf.keras.layers.LSTM(128, return_state=True, name='encoder')(encoder_embedded)
    encoder_state = [state_h, state_c]

    decoder_input = tf.keras.layers.Input(shape=(None,))
    decoder_embedded = tf.keras.layers.Embedding(input_dim=vocab_size, output_dim=128)(decoder_input)

    decoder_lstm = tf.keras.layers.LSTM(128, return_sequences=True, return_state=True, name='decoder')
    decoder_output, _, _ = decoder_lstm(decoder_embedded, initial_state=encoder_state)
    output = tf.keras.layers.Dense(20, activation='softmax')(decoder_output)


    model = tf.keras.Model(inputs=[encoder_input, decoder_input], outputs=output)
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    model.summary()

0 个答案:

没有答案