%config IPCompleter.greedy=True #TAB键代码自动提示
from tensorflow import keras
import tensorflow as tf
import numpy as np
print(tf.__version__)
一、构建模型
model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
#指定优化函数和损失函数
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)
三、训练模型
model.fit(xs, ys, epochs=2000)
Epoch 1/2000
1/1 [==============================] - 0s 997us/step - loss: 2.1879e-05
Epoch 2/2000
1/1 [==============================] - 0s 0s/step - loss: 2.1429e-05
Epoch 3/2000
....
Epoch 1998/2000
1/1 [==============================] - 0s 998us/step - loss: 1.0604e-11
Epoch 1999/2000
1/1 [==============================] - 0s 0s/step - loss: 1.0604e-11
Epoch 2000/2000
1/1 [==============================] - 0s 991us/step - loss: 1.0604e-11
<tensorflow.python.keras.callbacks.History at 0x2120b6ce130>
四、使用模型
print(model.predict([10.0]))
[[18.999987]]
网友评论