ظهور الخطأ ValueError: Input 0 of layer simple_rnn_20 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 16) أثناء تدريب شبكة RNNs
أحاول تدريب شبكة عصبية (هذه الشبكة عبارة عن تجميع طبقات RNN) لكن يظهر لي الخطأ في الكود التالي:
from keras.layers importDense,Embedding,SimpleRNNfrom keras.datasets import imdb
from keras.preprocessing import sequence
from keras.models importSequential
max_features =1000
maxlen =20
batch_size =64print('Loading data...')(input_train, y_train),(input_test, y_test)= imdb.load_data(
num_words=max_features)print(len(input_train),'train sequences')print(len(input_test),'test sequences')print('Pad sequences (samples x time)')
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)print('input_train shape:', input_train.shape)
model =Sequential()Loading data...25000 train sequences
25000 test sequences
Pad sequences (samples x time)
input_train shape:(25000,20)---------------------------------------------------------------------------ValueErrorTraceback(most recent call last)<ipython-input-45-586f07f93ca3>in<module>18 model.add(Embedding(max_features,16))19 model.add(SimpleRNN(16))--->20 model.add(SimpleRNN(16))21 model.add(Dense(1, activation='sigmoid'))22 model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])~\anaconda3\lib\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self,*args,**kwargs)515 self._self_setattr_tracking =False# pylint: disable=protected-access516try:-->517 result = method(self,*args,**kwargs)518finally:519 self._self_setattr_tracking = previous_value # pylint: disable=protected-access~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py in add(self, layer)221# If the model is being built continuously on top of an input layer:222# refresh its output.-->223 output_tensor = layer(self.outputs[0])224if len(nest.flatten(output_tensor))!=1:225raiseValueError(SINGLE_LAYER_OUTPUT_ERROR_MSG)~\anaconda3\lib\site-packages\tensorflow\python\keras\layers\recurrent.py in __call__(self, inputs, initial_state, constants,**kwargs)658659if initial_state isNoneand constants isNone:-->660return super(RNN, self).__call__(inputs,**kwargs)661662# If any of `initial_state` or `constants` are specified and are Keras~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self,*args,**kwargs)950if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):951return self._functional_construction_call(inputs, args, kwargs,-->952 input_list)953954# Maintains info about the `Layer.call` stack.~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)1089# Check input assumptions set after layer building, e.g. input shape.1090 outputs = self._keras_tensor_symbolic_call(->1091 inputs, input_masks, args, kwargs)10921093if outputs isNone:~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)820return nest.map_structure(keras_tensor.KerasTensor, output_signature)821else:-->822return self._infer_output_signature(inputs, args, kwargs, input_masks)823824def _infer_output_signature(self, inputs, args, kwargs, input_masks):~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)860# overridden).861# TODO(kaftan): do we maybe_build here, or have we already done it?-->862 self._maybe_build(inputs)863 outputs = call_fn(inputs,*args,**kwargs)864~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _maybe_build(self, inputs)2683ifnot self.built:2684 input_spec.assert_input_compatibility(->2685 self.input_spec, inputs, self.name)2686 input_list = nest.flatten(inputs)2687if input_list and self._dtype_policy.compute_dtype isNone:~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)221'expected ndim='+ str(spec.ndim)+', found ndim='+222 str(ndim)+'. Full shape received: '+-->223 str(tuple(shape)))224if spec.max_ndim isnotNone:225 ndim = x.shape.rank
ValueError:Input0 of layer simple_rnn_20 is incompatible with the layer: expected ndim=3, found ndim=2.Full shape received:(None,16)
model.add(Embedding(max_features,16))
model.add(SimpleRNN(16))
model.add(SimpleRNN(16))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(input_train, y_train,
epochs=2,
batch_size=128,
validation_split=0.2)
السؤال
Meezo ML
أحاول تدريب شبكة عصبية (هذه الشبكة عبارة عن تجميع طبقات RNN) لكن يظهر لي الخطأ في الكود التالي:
2 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.