【问题标题】:TypeError: model() got an unexpected keyword argument 'batch_size'TypeError:模型()得到了一个意外的关键字参数“batch_size”
【发布时间】:2021-05-03 23:53:15
【问题描述】:

我为 CNN 做了输入,但是我收到错误 TypeError: model() got an unexpected keyword argument 'batch_size' 让我在这里粘贴所有函数:

def model(x_train, num_labels, LSTM_units, num_conv_filters, batch_size, F, D):
"""
The proposed model with CNN layer, LSTM RNN layer and self attention layers.
Inputs:
- x_train: required for creating input shape for RNN layer in Keras
- num_labels: number of output classes (int)
- LSTM_units: number of RNN units (int)
- num_conv_filters: number of CNN filters (int)
- batch_size: number of samples to be processed in each batch
- F: the attention length (int)
- D: the length of the output (int) 
Returns
- model: A Keras model
"""
cnn_inputs = Input(shape=(x_train.shape[1], x_train.shape[2], 1), batch_size=batch_size, name='rnn_inputs')   # Gettting Error in this  line 
cnn_layer = Conv2D(num_conv_filters, kernel_size = (1, x_train.shape[2]), strides=(1, 1), padding='valid', data_format="channels_last")
cnn_out = cnn_layer(cnn_inputs)

sq_layer = Lambda(lambda x: K.squeeze(x, axis = 2))
sq_layer_out = sq_layer(cnn_out)

rnn_layer = LSTM(LSTM_units, return_sequences=True, name='lstm', return_state=True) #return_state=True
rnn_layer_output, _, _ = rnn_layer(sq_layer_out)

encoder_output, attention_weights = SelfAttention(size=F, num_hops=D, use_penalization=False, batch_size = batch_size)(rnn_layer_output)
dense_layer = Dense(num_labels, activation = 'softmax')
dense_layer_output = dense_layer(encoder_output)

model = Model(inputs=cnn_inputs, outputs=dense_layer_output)
print (model.summary())

return model

我已将注释放在“出现错误”的那一行。 这是回溯: Traceback(最近一次通话最后一次):

 File "C:\Program Files\JetBrains\PyCharm 2020.2.1\plugins\python\helpers\pydev\pydevd.py", line 1448, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2020.2.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Nafees Ahmed/PycharmProjects/encodingHumanActivity-master/codes/model_proposed/model_with_self_attn.py", line 133, in <module>
    rnn_model = model(x_train = X_train_, num_labels = NUM_LABELS, LSTM_units = LSTM_UNITS, \
TypeError: model() got an unexpected keyword argument 'batch_size'
python-BaseException

【问题讨论】:

  • 请说明您如何称呼该模型。我认为您使用batch_size= 参数调用。你做不到。如果你想预测大量数据 - 使用predict()
  • 我只是取batch_size=16,这里我先做训练数据。

标签: python tensorflow keras conv-neural-network batchsize


【解决方案1】:

batch_size 参数需要在model.fit() 函数中声明,而不是作为输入定义的一部分。

https://www.tensorflow.org/api_docs/python/tf/keras/Model

【讨论】:

  • 我已经从输入函数中删除了 batch_size,现在出现错误x = tf.placeholder(dtype, shape=shape, name=name) AttributeError: module 'tensorflow' has no attribute 'placeholder'
  • @Ahmad 试试this
  • @yudhiesh 我已经尝试过了,但我的命令行没有使用“conda”。
  • 检查您使用的 TensorFlow 版本。正如您在此答案中看到的那样:stackoverflow.com/a/61906983/13605826tf.placeholder 已移至 tf.compat.v1.placeholder
猜你喜欢
  • 2016-09-17
  • 2021-10-16
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多