-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-num.py
More file actions
22 lines (22 loc) · 1.21 KB
/
1-num.py
File metadata and controls
22 lines (22 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import tensorflow as tf
import numpy as np
from tensorflow import keras
#=======================================================================
# keras是一个将神经网络定义为一组连续层的框架
# 创建简单的神经网络(NN),它有1层,1个神经元,1个作为输入形状
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
#=======================================================================
## 损失函数根据已知的正确答案来衡量猜测的答案。也用来衡量预测的好坏 ( mean_squared_error 均方误差)
# 优化函数根据前面损失函数的猜测进行另一次猜测,试图使损失最小化并接近结果。它重复了许多次
# sgd =随机梯度下降
model.compile(optimizer = 'sgd', loss = 'mean_squared_error')
#=======================================================================
#填上数据
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
#=======================================================================
# 训练神经网络 500次
model.fit(xs, ys, epochs = 500)
# 验证
#=====================
print("最后的结果是======:",model.predict([10.0]))