繁体   English   中英

神经网络的输入形状

[英]Input shape of a neural network

我建立了一个神经网络,该网络应将Tweets分为四个类别之一。 但是我的输入形状似乎有问题。 train_features的形状也为(3817,4),train_label_onehot的形状也为(3817,4)。 Test_features的形状为(784,4)和test_label_onehot(784,4)。 Train_label_oehot和test_label_onehot是onehot编码的目标列表。 这是我的代码:

# Start neural network
network = models.Sequential()

# Add fully connected layer with a ReLU activation function
network.add(layers.Dense(200, activation='relu', input_shape=(3817,)))

# Add fully connected layer with a ReLU activation function
network.add(layers.Dense(100, activation='relu'))

# Add fully connected layer with a softmax activation function for multiclass problems
network.add(layers.Dense(4, activation='softmax'))

network.summary()

# Compile neural network
network.compile(loss='sparse_categorical_crossentropy', # Cross-entropy
                optimizer='adam', # Root Mean Square Propagation
                 # Accuracy performance metric
                metrics=['accuracy'])


# Train neural network
history = network.fit(train_features, # Features
                      train_label_onehot, # Target vector, shape(3817, 4)
                      epochs=10, 
                      verbose=4, 
                      batch_size=100, # Number of observations per batch
                      validation_data=(test_features, test_label_onehot)) # Data for evaluation # test_label_onehot shape(784, 4)

network.summary()给了我这个:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_775 (Dense)            (None, 200)               763600    
_________________________________________________________________
dense_776 (Dense)            (None, 100)               20100     
_________________________________________________________________
dense_777 (Dense)            (None, 4)                 404       
=================================================================
Total params: 784,104
Trainable params: 784,104
Non-trainable params: 0

错误显示:

ValueError: Error when checking input: expected dense_775_input to have shape (3817,) but got array with shape (4,)

有人可以帮我弄这个吗?

更改第一层的输入形状以匹配其中一个数据

# Add fully connected layer with a ReLU activation function
network.add(layers.Dense(200, activation='relu', input_shape=(4,)))

或更明确

# Add fully connected layer with a ReLU activation function
network.add(layers.Dense(200, activation='relu', input_dim=4))

或更一般地

# Add fully connected layer with a ReLU activation function
network.add(layers.Dense(200, activation='relu', input_dim=train_features.shape[1]))

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM