tensorflow / tensorflow

An Open Source Machine Learning Framework for Everyone
https://tensorflow.org
Apache License 2.0
186.08k stars 74.27k forks source link

Receiving the error: "ValueError: Input 0 of layer sequential_8 is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)" #46076

Closed p-deol closed 3 years ago

p-deol commented 3 years ago

Hello,

I would like to apologize 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 GitHub, but have found many answers to posts on this forum very helpful to me. 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:\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 graphfunction, = 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)

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.

Saduf2019 commented 3 years ago

@p-deol This question is better asked on StackOverflow since it is not a bug or feature request. There is also a larger community that reads questions there. please move this to close status once issue is created on SO.

p-deol commented 3 years ago

Thank you. I received a response from Andrey on StackOverflow that solved my issue. Response was as follows:

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