神经网络算法代码python
时间: 2023-12-26 12:28:41 浏览: 90
以下是一个使用Keras库编写神经网络算法的Python代码示例:
```python
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
# 创建一个Sequential模型
model = Sequential()
# 添加输入层和隐藏层
model.add(Dense(units=64, activation='relu', input_dim=100))
# 添加输出层
model.add(Dense(units=10, activation='softmax'))
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# 生成随机输入数据和标签
data = np.random.random((1000, 100))
labels = np.random.randint(10, size=(1000, 1))
# 将标签转换为分类的one-hot编码
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)
# 训练模型
model.fit(data, one_hot_labels, epochs=10, batch_size=32)
# 使用模型进行预测
predictions = model.predict(data)
```
这段代码使用Keras库创建了一个简单的神经网络模型,包括一个输入层、一个隐藏层和一个输出层。模型使用随机生成的数据进行训练,并使用训练好的模型进行预测。
阅读全文
相关推荐
















