簡體   English   中英

“ValueError:……與圖層不兼容:輸入形狀的預期軸 -1 的值為 8,但接收到的輸入形狀為(無、7、169)”

[英]“ValueError: …incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)”

我在 GitHub 上問了這個問題,我想我也會在這里發布。 如果這張貼在錯誤的位置,我深表歉意。 如果是這樣的話,任何關於我可以在哪里提出這個問題的指導將不勝感激。 我是編碼和 StackOverflow 的新手,但在這個論壇上發現很多帖子的答案非常有幫助。 我正在嘗試設置一個代碼來利用 Keras model 進行深度學習,使用 3d 數據集,我不確定如何處理我收到的錯誤(在標題中)。 我在這個論壇上發現了一個類似的錯誤,但響應是它將在 TensorFlow v 2.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 是一個 float64 數組,大小為 (152,7,169),Y 是一個 int32 數組,大小為 (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(最近一次調用最后一次):

文件“C:\Users\pdeol\Documents\CodingPractice\KerasCombined.py”,第 171 行,model.fit(X_train, y_train, epochs=150, batch_size=10)

文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py”,第 1100 行,適合 tmp_logs = self.train_function(iterator)

文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py”,第 828 行,調用結果 = self._call(*args, **kwds)

文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py”,第 871 行,在 _call self._initialize(args, kwds, add_initializers_to=initializers)

文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py”,第 725 行,_initialize self._stateful_fn._get_concrete_function_internal_garbage_collected(#pylint: disable=protected-access

文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\function.py”,第 2969 行,在 _get_concrete_function_internal_garbage_collected graph_function,_ = self._maybe_define_function(args, kwargs)

文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\function.py”,第 3361 行,在 _maybe_define_function graph_function = self._create_graph_function(args, kwargs)

_create_graph_function func_graph_module.func_graph_from_py_func(

文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py”,第 990 行,在 func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs)

文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py”,第 634 行,在 Wrapped_fn out = weak_wrapped_fn().wrapped(*args, **kwds)

包裝器中的文件“C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py”,第 977 行引發 e.ag_error_metadata.to_exception(e)

ValueError:在用戶代碼中:

C:\Users\pdeol\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function * return step_function(self, iterator) C:\Users\-pdeol\anacondalib packages\tensorflow\python\keras\engine\training.py:795 step_function ** 輸出 = model.distribute_strategy.run(run_step, args=(data,)) C:\libUsers\pdeol\anacondacondaconda tensorflow\python\distribute\distribute_lib.py:1259 運行返回 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 fn(*args_plica, return **kwargs) Z0D 61F8370CAD1D412F80B84D143E1257Z:\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\站點包\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調用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引發價值錯誤(

ValueError:層順序的輸入 0 與層不兼容:輸入形狀的預期軸 -1 具有值 7,但接收到的輸入具有形狀(無、7、169)

上述錯誤是我每次運行此代碼時收到的錯誤。 我剛剛重新啟動軟件並再次運行它,除了上述錯誤之外,我立即收到以下錯誤:

2020-12-30 10:17:26.556667:W tensorflow/stream_executor/platform/default/dso_loader.cc:60] 無法加載動態庫“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:我 tensorflow/compiler/jit/xla_cpu_device.cc:41] 未創建 XLA 設備,未設置 tf_xla_enable_xla_devices 2020-12-30 10:26:08.616994:W tensorflow/stream_executor/platform/ default/dso_loader.cc:60] 無法加載動態庫“nvcuda.dll”; dlerror: nvcuda.dll 未找到 2020-12-30 10:26:08.617895: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] 調用 cuInit 失敗:未知錯誤 (303) 20260-12-30 10:: 08.628902:i TensorFlow/stream_executor/cuda/cuda_diagnostics.cc:169]檢索ZA33B7775E5E5F9B504D2D038ECA4FF28EACA4FF28EASTIC HOST的診斷信息:LT-B831B58344ERFLOFE:LT-B831B58344ENORS 3 2020-2020202020202020202020202020202020202020202020202020202020202020202020號: : LT-B831B58344E3 2020-12-30 10:26:08.634489: I tensorflow/core/platform/cpu_feature_guard.cc:142] 這個 TensorFlow 二進制文件使用 oneAPI 深度神經網絡庫 (oneDNN) 進行了優化,以在性能方面使用以下 CPU 指令-關鍵操作:AVX2 要在其他操作中啟用它們,請使用適當的編譯器標志重建 TensorFlow。 2020-12-30 10:26:08.636834:我 tensorflow/compiler/jit/xla_gpu_device.cc:99] 未創建 XLA 設備,未設置 tf_xla_enable_xla_devices 2020-12-30 10:26:08.863911:我 tensorflow/compiler/mlir/ mlir_graph_optimization_pass.cc:116] 未啟用任何 MLIR 優化通道(已注冊 2)

我不確定這是相關的,還是完全獨立的問題。 您可以提供的任何信息或指導將不勝感激。

第一個 Dense 層的 Input_dim 參數是 7。這意味着您的 model 期望數據的最后一個維度是 7。但是您正在輸入 (152, 7, 169),其中 169 是最后一個維度。

嘗試轉置它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM