这是我对此程序的代码。它工作正常,但突然无法工作,请任何人都可以解决此问题
model = Sequential()
print(nb_filters[0], 'filters')
print('input shape', img_rows, 'rows', img_cols, 'cols', patch_size, 'patchsize')
model.add(Convolution3D(
nb_filters[0],
kernel_dim1=1, # depth
kernel_dim2=nb_conv[0], # rows
kernel_dim3=nb_conv[1], # cols
input_shape=(1, img_rows, img_cols, patch_size),
activation='relu'
))
model.add(MaxPooling3D(pool_size=(1, nb_pool[0], nb_pool[0])))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, init='normal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(nb_classes,init='normal'))
model.add(Activation('softmax'))
#optimizer adam,sgd,RMSprop,Adagrad,Adadelta,Nadam,
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
此错误在我的程序中创建。解决我多次搜索却无法解决的问题时,我不了解的问题是什么?--------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-112-671e85975992> in <module>
13 x = Dense(nb_classes, activation='softmax')(x)
14
---> 15 custom_model = Model(input=resnet_model.input, output=x)
16
17 for layer in custom_model.layers[:7]:
/usr/local/lib/python3.8/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/training.py in __init__(self, *args, **kwargs)
259 # self.trainable_weights
260 # self.non_trainable_weights
--> 261 generic_utils.validate_kwargs(kwargs, {'trainable', 'dtype', 'dynamic',
262 'name', 'autocast'})
263 super(Model, self).__init__(**kwargs)
/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message)
776 for kwarg in kwargs:
777 if kwarg not in allowed_kwargs:
--> 778 raise TypeError(error_message, kwarg)
779
780
TypeError: ('Keyword argument not understood:', 'input')
最佳答案
根据Dr. Snoopy的建议,tf.keras.Model
的参数为inputs
和outputs
,但是您分别在input
中将其分别作为output
和custom_model = Model(input=resnet_model.input, output=x)
传递。
重现该错误的代码-
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
X1 = tf.constant([2, 3, 4, 5, 6, 7])
X2 = tf.constant([2, 3, 4, 5, 6, 7])
yTrain = tf.constant([4, 6, 8, 10, 12, 14])
input1 = keras.Input(shape=(1,))
input2 = keras.Input(shape=(1,))
x = layers.concatenate([input1, input2])
x = layers.Dense(8, activation='relu')(x)
outputs = layers.Dense(2)(x)
mlp = keras.Model(input = [input1, input2], output = outputs)
mlp.summary()
mlp.compile(loss='mean_squared_error',
optimizer='adam', metrics=['accuracy'])
mlp.fit([X1, X2], yTrain, batch_size=1, epochs=10, validation_split=0.2,
shuffle=True)
mlp.evaluate([X1, X2], yTrain)
输出----------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-bec9ebbd1faf> in <module>()
14 x = layers.Dense(8, activation='relu')(x)
15 outputs = layers.Dense(2)(x)
---> 16 mlp = keras.Model(input = [input1, input2], output = outputs)
17
18 mlp.summary()
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message)
776 for kwarg in kwargs:
777 if kwarg not in allowed_kwargs:
--> 778 raise TypeError(error_message, kwarg)
779
780
TypeError: ('Keyword argument not understood:', 'input')
要解决该错误,请将参数更改为inputs
和outputs
。固定代码-
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
X1 = tf.constant([2, 3, 4, 5, 6, 7])
X2 = tf.constant([2, 3, 4, 5, 6, 7])
yTrain = tf.constant([4, 6, 8, 10, 12, 14])
input1 = keras.Input(shape=(1,))
input2 = keras.Input(shape=(1,))
x = layers.concatenate([input1, input2])
x = layers.Dense(8, activation='relu')(x)
outputs = layers.Dense(2)(x)
mlp = keras.Model(inputs = [input1, input2], outputs = outputs)
mlp.summary()
mlp.compile(loss='mean_squared_error',
optimizer='adam', metrics=['accuracy'])
mlp.fit([X1, X2], yTrain, batch_size=1, epochs=10, validation_split=0.2,
shuffle=True)
mlp.evaluate([X1, X2], yTrain)
输出-Model: "functional_5"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_6 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_7 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
concatenate_34 (Concatenate) (None, 2) 0 input_6[0][0]
input_7[0][0]
__________________________________________________________________________________________________
dense_4 (Dense) (None, 8) 24 concatenate_34[0][0]
__________________________________________________________________________________________________
dense_5 (Dense) (None, 2) 18 dense_4[0][0]
==================================================================================================
Total params: 42
Trainable params: 42
Non-trainable params: 0
__________________________________________________________________________________________________
Epoch 1/10
4/4 [==============================] - 0s 32ms/step - loss: 54.3236 - accuracy: 0.0000e+00 - val_loss: 169.3114 - val_accuracy: 0.0000e+00
Epoch 2/10
4/4 [==============================] - 0s 6ms/step - loss: 53.4965 - accuracy: 0.0000e+00 - val_loss: 167.0008 - val_accuracy: 0.0000e+00
Epoch 3/10
4/4 [==============================] - 0s 6ms/step - loss: 52.7413 - accuracy: 0.0000e+00 - val_loss: 164.6473 - val_accuracy: 0.0000e+00
Epoch 4/10
4/4 [==============================] - 0s 6ms/step - loss: 51.8159 - accuracy: 0.0000e+00 - val_loss: 162.4427 - val_accuracy: 0.0000e+00
Epoch 5/10
4/4 [==============================] - 0s 6ms/step - loss: 51.0917 - accuracy: 0.0000e+00 - val_loss: 160.1798 - val_accuracy: 0.0000e+00
Epoch 6/10
4/4 [==============================] - 0s 6ms/step - loss: 50.4425 - accuracy: 0.0000e+00 - val_loss: 157.8355 - val_accuracy: 0.0000e+00
Epoch 7/10
4/4 [==============================] - 0s 6ms/step - loss: 49.5709 - accuracy: 0.0000e+00 - val_loss: 155.6147 - val_accuracy: 0.0000e+00
Epoch 8/10
4/4 [==============================] - 0s 6ms/step - loss: 48.7816 - accuracy: 0.0000e+00 - val_loss: 153.4298 - val_accuracy: 0.0000e+00
Epoch 9/10
4/4 [==============================] - 0s 6ms/step - loss: 47.9975 - accuracy: 0.0000e+00 - val_loss: 151.2858 - val_accuracy: 0.0000e+00
Epoch 10/10
4/4 [==============================] - 0s 6ms/step - loss: 47.3943 - accuracy: 0.0000e+00 - val_loss: 149.0254 - val_accuracy: 0.0000e+00
1/1 [==============================] - 0s 2ms/step - loss: 80.9333 - accuracy: 0.0000e+00
[80.93333435058594, 0.0]
关于python-3.x - TypeError:('Keyword argument not understood:', 'input'),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63497874/