简体   繁体   English

设置tensorflow序列模型输入层的形状

[英]Setting the shape of tensorflow sequential model input layer

I'm trying to build a model for multi class classification, but I don't understand how to set the correct input shape.我正在尝试为多类分类构建模型,但我不明白如何设置正确的输入形状。 I have a training set with shape (5420, 212) and this is the model I built:我有一个形状为(5420, 212)的训练集,这是我构建的模型:

model = models.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape = (5420,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(5, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
history = model.fit(X_train, y_train, epochs=20, batch_size=512)

When I run it I get the error:当我运行它时,我收到错误:

ValueError: Input 0 of layer sequential_9 is incompatible with the layer: expected axis -1 of input shape to have value 5420 but received input with shape (None, 212)

Why?为什么? Isn't the input value correct?输入值不正确吗?

The input shape should be equal to the inputs X 's second dimension, while the output shape should be equal to the outputs Y 's second dimension (assuming that both X and Y are 2-dimensional, ie that they don't have higher dimensions).输入形状应等于输入X的第二维,而输出形状应等于输出Y的第二维(假设XY都是二维的,即它们没有更高的方面)。

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.datasets import make_classification
from sklearn.preprocessing import OneHotEncoder
tf.random.set_seed(0)

# generate some data
X, y = make_classification(n_classes=5, n_samples=5420, n_features=212, n_informative=212, n_redundant=0, random_state=42)
print(X.shape, y.shape)
# (5420, 212) (5420,)

# one-hot encode the target
Y = OneHotEncoder(sparse=False).fit_transform(y.reshape(-1, 1))
print(X.shape, Y.shape)
# (5420, 212) (5420, 5)

# extract the input and output shapes
input_shape = X.shape[1:]  # the input shape is X's second dimension
output_shape = Y.shape[1]  # the output shape is Y's second dimension
print(input_shape, output_shape)
# (212,) 5

# define the model
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=input_shape))
model.add(Dense(64, activation='relu'))
model.add(Dense(output_shape, activation='softmax'))

# compile the model
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

# fit the model
history = model.fit(X, Y, epochs=3, batch_size=512)
# Epoch 1/3
# 11/11 [==============================] - 0s 1ms/step - loss: 4.8206 - accuracy: 0.2208
# Epoch 2/3
# 11/11 [==============================] - 0s 1ms/step - loss: 2.8060 - accuracy: 0.3229
# Epoch 3/3
# 11/11 [==============================] - 0s 1ms/step - loss: 2.0705 - accuracy: 0.3989

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow Model 输入形状错误:层顺序_11的输入0与层不兼容:未定义等级,但层需要定义的等级 - Tensorflow Model Input Shape Error: Input 0 of layer sequential_11 incompatible with layer: rank undefined, but the layer requires a defined rank 顺序 Keras 模型的设置形状和输入形状的混淆 - Confusion in setting shape and input shape of a sequential Keras model Tensorflow Conv2D 层 input_shape 配置错误:ValueError: Input 0 of layer "sequential" is in compatible with the layer: - Tensorflow Conv2D layers input_shape configuration error: ValueError: Input 0 of layer "sequential" is incompatible with the layer: 顺序 model tensorflow 中的自定义层 - Custom layer in sequential model tensorflow Keras - ValueError:Sequential模型中的第一层必须得到`input_shape`或`batch_input_shape`参数 - Keras - ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer Keras 顺序模型输入形状 - Keras Sequential model input shape Keras Sequential 模型输入层 - Keras Sequential model input layer TensorFlow 层子类输入形状 - TensorFlow layer subclass input shape 警告:tensorflow:没有传递给第一层的`input_shape`的顺序模型无法重新加载其优化器状态 - WARNING:tensorflow:Sequential models without an `input_shape` passed to the first layer cannot reload their optimizer state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM