ValueError:…与层不兼容:输入形状的轴1应具有值8,但接收到形状为的输入(无、7、169)

2024-10-04 09:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在GitHub上问了这个问题,我想我也会把它贴在这里。我很抱歉,如果这是张贴在错误的位置。如果是这样的话,我将非常感谢任何关于我在哪里可以问这个问题的指导。我对编码和StackOverflow还不熟悉,但在这个论坛上的帖子中,我发现很多答案都非常有用。我正在尝试设置一个代码,以利用Keras模型对3d数据集进行深入学习,并且不确定如何处理我收到的错误(在标题中)。我在这个论坛上发现了一个类似的错误,但得到的回应是,它将在TensorFlow V2.1中修复。我使用的是v2.4.0,所以我想我可能在这里做错了什么。我使用的代码如下:

# 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))

有关变量的信息如下:

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

有关版本的信息如下:

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

我得到的完整错误如下:

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

File "C:\Users\pdeol\Documents\CodingPractice\KerasCombined.py", line 171, in model.fit(X_train, y_train, epochs=150, batch_size=10)

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit tmp_logs = self.train_function(iterator)

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in call result = self._call(*args, **kwds)

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 871, in _call self._initialize(args, kwds, add_initializers_to=initializers)

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 725, in _initialize self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 2969, in _get_concrete_function_internal_garbage_collected graph_function, _ = self._maybe_define_function(args, kwargs)

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3361, in _maybe_define_function graph_function = self._create_graph_function(args, kwargs)

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\function.py", line 3196, in _create_graph_function func_graph_module.func_graph_from_py_func(

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py", line 990, in func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs)

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py", line 634, in wrapped_fn out = weak_wrapped_fn().wrapped(*args, **kwds)

File "C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py", line 977, in wrapper raise e.ag_error_metadata.to_exception(e)

ValueError: in user code:

C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function * return step_function(self, iterator) C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica return fn(*args, **kwargs) C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step ** outputs = model.train_step(data) C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:754 train_step y_pred = self(x, training=True) C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:998 call input_spec.assert_input_compatibility(self.input_spec, inputs, self.name) C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\input_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)

上面的错误是我每次运行此代码时收到的错误。我刚刚重新启动了软件并再次运行,除了上面的错误,我还收到了下面的错误:

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)

我不确定这是相关的,还是完全独立的问题。如果您能提供任何信息或指导,我们将不胜感激


Tags: inpyselfmodellibpackagestensorflowsite
1条回答
网友
1楼 · 发布于 2024-10-04 09:31:52

第一个密集层的输入参数为7。这意味着您的模型期望数据的最后一个维度是7。但是你正在进食(152,7169),其中169是最后一个维度

尝试将其转置:

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

相关问题 更多 >