梯度算法求ax+b

目的:

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
agemaney
046
169
2710
3812
4913
51016
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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值