# Tensorflow: The channel dimension of the inputs should be defined

## Question:

I am new to Tensorflow, and am trying to train a specific deep learning neural network. I am using Tensorflow (2.11.0) to get a deep neural network model which is described below. The data which I use is also given below:

**Data:**

Here is some example data. For sake of ease we can consider 10 samples in data. Here, each sample has shape: `(128,128)`

.

One can consider the below code as example training data.

```
x_train = np.random.rand(10, 128, 128, 1)
```

**Normalization layer:**

```
normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(x_train)
```

**Build model:**

```
def build_and_compile_model(norm):
model = tf.keras.Sequential([
norm,
layers.Conv2D(128, 128, activation='relu'),
layers.Conv2D(3, 3, activation='relu'),
layers.Flatten(),
layers.Dense(units=32, activation='relu'),
layers.Dense(units=1)
])
model.compile(loss='mean_absolute_error', optimizer=tf.keras.optimizers.Adam(0.001))
return model
```

When I do

```
dnn_model = build_and_compile_model(normalizer)
dnn_model.summary()
```

I get the below error:

```
ValueError: The channel dimension of the inputs should be defined. The input_shape received is (None, None, None, None), where axis -1 (0-based) is the channel dimension, which found to be `None`.
```

**What am I doing wrong here?**

I have tried to get insights from this, this, this and this. But, I have not found a workable solution yet.

What should I do to remove the error and get the model to work?

I will appreciate any help.

## Answers:

Define the input shape directly in the normalization layer (or add an `Input`

layer), since it cannot be inferred directly:

```
import numpy as np
import tensorflow as tf
x_train = np.random.rand(10, 128, 128, 1)
normalizer = tf.keras.layers.Normalization(input_shape=[128, 128, 1], axis=-1)
normalizer.adapt(x_train)
def build_and_compile_model(norm):
model = tf.keras.Sequential([
norm,
tf.keras.layers.Conv2D(64, 64, activation='relu'),
tf.keras.layers.Conv2D(3, 3, activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units=32, activation='relu'),
tf.keras.layers.Dense(units=1)
])
model.compile(loss='mean_absolute_error', optimizer=tf.keras.optimizers.Adam(0.001))
return model
dnn_model = build_and_compile_model(normalizer)
dnn_model.summary()
```

Also, your model does not work as it is, you are using a kernel size of 128 in your first `Conv2D`

layer and then another `Conv2D`

layer with a kernel size of 3 but your data has the shape `(10, 128, 128, 1)`

. I changed it to make your code executable.