Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

python - "ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)"

I asked this question on GitHub, and I thought I would post it here as well. My apologies if this is posted in the wrong location. If that is the case, any guidance as to where I can ask this question would be greatly appreciated. I am new to coding and StackOverflow, but have found many answers to posts on this forum very helpful. I am trying to setup a code to utilize the Keras model for deep learning with a 3d dataset, and am uncertain how to deal with the error I'm receiving (in the title). I have found a similar error reported on this forum, but the response was that it would be fixed in TensorFlow v 2.1. I am using v2.4.0, so I expect I'm probably doing something wrong here. The code I am using is as follows:

# define the keras model
model = Sequential()
model.add(Dense(12, input_dim=7, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# separate the data
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=0)

# fit the keras model on the dataset
model.fit(X_train, y_train, epochs=150, batch_size=10)

# evaluate the keras model
_, accuracy = model.evaluate(X_test, y_test)
print('Accuracy: %.2f' % (accuracy*100))

Information on the variables as follows:

X is an array of float64, with size (152,7,169), Y is an array of int32, with size (152,)

Information on versions as follows:

Python version 3.8.5, Keras version 2.4.3, TensorFlow version 2.4.0, Windows 10, Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz 1.90 GHz (AVX capable), Spyder version 4.2.0

The full error I am getting is as follows:

Epoch 1/150 Traceback (most recent call last):

File "C:UserspdeolDocumentsCodingPracticeKerasCombined.py", line 171, in model.fit(X_train, y_train, epochs=150, batch_size=10)

File "C:Userspdeolanaconda3libsite-packagesensorflowpythonkerasengineraining.py", line 1100, in fit tmp_logs = self.train_function(iterator)

File "C:Userspdeolanaconda3libsite-packagesensorflowpythoneagerdef_function.py", line 828, in call result = self._call(*args, **kwds)

File "C:Userspdeolanaconda3libsite-packagesensorflowpythoneagerdef_function.py", line 871, in _call self._initialize(args, kwds, add_initializers_to=initializers)

File "C:Userspdeolanaconda3libsite-packagesensorflowpythoneagerdef_function.py", line 725, in _initialize self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access

File "C:Userspdeolanaconda3libsite-packagesensorflowpythoneagerfunction.py", line 2969, in _get_concrete_function_internal_garbage_collected graph_function, _ = self._maybe_define_function(args, kwargs)

File "C:Userspdeolanaconda3libsite-packagesensorflowpythoneagerfunction.py", line 3361, in _maybe_define_function graph_function = self._create_graph_function(args, kwargs)

File "C:Userspdeolanaconda3libsite-packagesensorflowpythoneagerfunction.py", line 3196, in _create_graph_function func_graph_module.func_graph_from_py_func(

File "C:Userspdeolanaconda3libsite-packagesensorflowpythonframeworkfunc_graph.py", line 990, in func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs)

File "C:Userspdeolanaconda3libsite-packagesensorflowpythoneagerdef_function.py", line 634, in wrapped_fn out = weak_wrapped_fn().wrapped(*args, **kwds)

File "C:Userspdeolanaconda3libsite-packagesensorflowpythonframeworkfunc_graph.py", line 977, in wrapper raise e.ag_error_metadata.to_exception(e)

ValueError: in user code:

C:Userspdeolanaconda3libsite-packagesensorflowpythonkerasengineraining.py:805 train_function * return step_function(self, iterator) C:Userspdeolanaconda3libsite-packagesensorflowpythonkerasengineraining.py:795 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) C:Userspdeolanaconda3libsite-packagesensorflowpythondistributedistribute_lib.py:1259 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) C:Userspdeolanaconda3libsite-packagesensorflowpythondistributedistribute_lib.py:2730 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) C:Userspdeolanaconda3libsite-packagesensorflowpythondistributedistribute_lib.py:3417 _call_for_each_replica return fn(*args, **kwargs) C:Userspdeolanaconda3libsite-packagesensorflowpythonkerasengineraining.py:788 run_step ** outputs = model.train_step(data) C:Userspdeolanaconda3libsite-packagesensorflowpythonkerasengineraining.py:754 train_step y_pred = self(x, training=True) C:Userspdeolanaconda3libsite-packagesensorflowpythonkerasenginease_layer.py:998 call input_spec.assert_input_compatibility(self.input_spec, inputs, self.name) C:Userspdeolanaconda3libsite-packagesensorflowpythonkerasengineinput_spec.py:255 assert_input_compatibility raise ValueError(

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 7 but received input with shape (None, 7, 169)

The above error is what I receive every time I run this code. I just restarted the software and ran it again, and in addition to the above error, I received the following error immediately below:

2020-12-30 10:17:26.556667: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2020-12-30 10:17:26.557141: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. 2020-12-30 10:26:08.613023: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set 2020-12-30 10:26:08.616994: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found 2020-12-30 10:26:08.617895: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303) 2020-12-30 10:26:08.628902: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: LT-B831B58344E3 2020-12-30 10:26:08.629094: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: LT-B831B58344E3 2020-12-30 10:26:08.634489: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2020-12-30 10:26:08.636834: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set 2020-12-30 10:26:08.863911: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)

I'm not sure if this is related, or a completely separate issue. Any information or guidance you can provide would be greatly appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Input_dim argument of the first Dense layer is 7. It means that your model expecting the last dimension of your data is 7. But you are feeding (152, 7, 169), where 169 is the last dimension.

Try to transpose it:

X = tf.transpose(X, (0, 2, 1))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...