python 神经网络拟合二输入,一输出曲面的代码算法
时间: 2023-03-27 16:02:41 浏览: 65
以下是一个简单的 Python 神经网络拟合二输入,一输出曲面的代码算法:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from keras.models import Sequential
from keras.layers import Dense
# 生成数据
x1 = np.random.rand(100)
x2 = np.random.rand(100)
y = np.sin(x1 * np.pi) + np.cos(x2 * np.pi)
# 构建模型
model = Sequential()
model.add(Dense(10, input_dim=2, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='adam')
# 训练模型
model.fit(np.column_stack((x1, x2)), y, epochs=100, batch_size=32)
# 预测曲面
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x1_test = np.linspace(, 1, 100)
x2_test = np.linspace(, 1, 100)
X1, X2 = np.meshgrid(x1_test, x2_test)
Y = model.predict(np.column_stack((X1.flatten(), X2.flatten())))
Y = Y.reshape(X1.shape)
ax.plot_surface(X1, X2, Y)
plt.show()
```
这个代码使用了 Keras 库来构建神经网络模型,使用了随机生成的数据来训练模型,并使用 matplotlib 库来可视化预测的曲面。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)