英文:
Tensorflow: The channel dimension of the inputs should be defined
问题
抱歉,以下是翻译的部分内容:
"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
."
"我正在尝试训练一个特定的深度学习神经网络,但当我构建模型时,出现了以下错误:'ValueError: 输入的通道维度应该被定义。接收到的输入形状是(None,None,None,None),其中轴-1(从0开始)是通道维度,但发现为'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."
"我会感激任何帮助。"
英文:
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.
答案1
得分: 1
在规范化层中直接定义输入形状(或添加一个Input
层),因为它无法直接推断:
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()
另外,您的模型目前无法正常工作,因为您在第一个Conv2D
层中使用了大小为128的内核,然后又使用了另一个内核大小为3的Conv2D
层,但您的数据形状是(10, 128, 128, 1)
。我已经进行了修改以使您的代码可执行。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论