目的:
tf.kernel的一般步骤 梯度算法的原理
import tensorflow as tf
print('ts-version:{}'.format(tf.__version__))
ts-version:2.3.0
import pandas as pd
data = pd.read_csv('C:/Users/DengSer/Desktop/brand.csv')
data
age | maney | |
---|---|---|
0 | 4 | 6 |
1 | 6 | 9 |
2 | 7 | 10 |
3 | 8 | 12 |
4 | 9 | 13 |
5 | 10 | 16 |
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(data.age,data.maney)
<matplotlib.collections.PathCollection at 0x1d9f7efe588>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fqf8lAPK-1599789210176)(output_6_1.png)]
x=data.age
y=data.maney
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1,input_shape=(1,)))
model.summary() #ax+b
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 1) 2
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________
model.compile(optimizer='adam',#梯度算法模型
loss='mse'
)
history= model.fit(x,y,epochs=5000)#训练次数
Epoch 1/5000
1/1 [==============================] - 0s 23ms/step - loss: 422.4163
Epoch 2/5000
1/1 [==============================] - 0s 0s/step - loss: 422.0647
Epoch 3/5000
。
。
。
Epoch 4996/5000
1/1 [==============================] - 0s 998us/step - loss: 0.8053
Epoch 4997/5000
1/1 [==============================] - 0s 991us/step - loss: 0.8053
Epoch 4998/5000
1/1 [==============================] - 0s 997us/step - loss: 0.8052
Epoch 4999/5000
1/1 [==============================] - 0s 998us/step - loss: 0.8052
Epoch 5000/5000
1/1 [==============================] - 0s 998us/step - loss: 0.8051
model.predict(x)
array([[ 7.111453],
[ 9.518102],
[10.721427],
[11.924751],
[13.128076],
[14.3314 ]], dtype=float32)
model.predict(pd.Series([20]))#good good study more more money
array([[26.364645]], dtype=float32)