NN神经网络模型构建python
时间: 2023-08-03 22:58:09 浏览: 157
在Python中构建神经网络模型,可以使用各种深度学习库,如TensorFlow、Keras、PyTorch等。以下是使用TensorFlow和Keras构建神经网络模型的示例代码:
```python
import tensorflow as tf
from tensorflow import keras
# 构建神经网络模型
model = keras.Sequential([
keras.layers.Dense(64, activation='relu', input_shape=(784,)),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 加载数据集并进行预处理
(train_images, train_labels), (test_images, test_labels) = keras.datasets.mnist.load_data()
train_images = train_images.reshape((60000, 784))
test_images = test_images.reshape((10000, 784))
train_images = train_images / 255.0
test_images = test_images / 255.0
# 训练模型
model.fit(train_images, train_labels, epochs=10, batch_size=32)
# 评估模型
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)
```
这是一个简单的多层感知器模型,用于识别手写数字。您可以根据您的需求自定义模型的架构和参数,并使用不同的数据集进行训练和评估。
阅读全文
相关推荐


















