TensorFlow笔记
[by_041]
TensorFlow是基于 Tensor(张量)计算的一种 深度学习库
参考B站视频
一个博主,他最开始的博文全是关于TF的(至少22篇)
Tensorflow 1 的官方文档 用py2的
文章目录
- TensorFlow笔记
*
–
+
*
–
+ [by_041] - @[toc]
- 安装
- 语法基础
- 使用基础
– - 跟一个叫itluojie博主做TF
–
安装
- 使用
Anaconda Prompt
- 先升级pip
python -m pip install --upgrade pip
- 再用镜像站安装
pip install tensorflow-cpu -i https://pypi.douban.com/simple/
不知道啥玩意的(跳过)
pip install numpy pandas matplotlib sklearn tensorflow==2.0.0-alpha0 -i http://pypi.douban.com/simple/
安装notebook配合使用体验更佳(体现在Jupyter中的使用)
pip install notebook -i http://pypi.douban.com/simple/
- 启用
activate tensorflow
禁用
deactivate
- 快速安装
(若需要升级pip)python -m pip install --upgrade pip
pip install tensorflow-cpu -i https://pypi.douban.com/simple/
pip install notebook -i http://pypi.douban.com/simple/
activate tensorflow
- 尝试以下程序。如果你能运行它,它就可以了。
[En]
try the following program. If you can run it, it will be OK.*
import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'import tensorflow as tft0 = tf.constant(3, dtype=tf.int32)t1 = tf.constant([3., 4.1, 5.2], dtype=tf.float32)t2 = tf.constant([['Apple', 'Orange'], ['Potato', 'Tomato']], dtype=tf.string)t3 = tf.constant([[[5], [6], [7]], [[4], [3], [2]]])print('==========================================================================')print(t0)print('==========================================================================')print(t1)print('==========================================================================')print(t2)print('==========================================================================')print(t3)print('==========================================================================')
语法基础
TensorFlow是基于 Tensor(张量)计算的一种深度学习库,其基础语法都有所体现
使用基础
数据处理基础
包括用
pandas
读取数据
基于 pandas
进行读取数据
import pandas as pd
data = pd.read_csv('C:\\Users\\15614\\Desktop\\应用基础\\数模实战\\数据\\一手粉丝量 - 90 days.csv')
print(data)
基于 matplotlib
进行数据绘图
import matplotlib.pyplot as plt
plt.scatter(data.time,data.fans)
Tensorflow 2.0 tf.keras
官方推荐的首选
一次完整的线性回归分析
import pandas as pd
data = pd.read_csv('C:\\Users\\15614\\Desktop\\应用基础\\数模实战\\数据\\一手粉丝量 - 90 days.csv')
print(data)
import matplotlib.pyplot as plt
plt.scatter(data.time,data.fans)
x=data.time
y=data.fans
import tensorflow as tf
model=tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1,input_shape=(1,)))
print('==========================================================================')
print(model.summary())
print('==========================================================================')
model.compile(optimizer='adam',
loss='mse'
)
history=model.fit(x,y,epochs=500)
predict_1=model.predict(x)
predict_2=model.predict(pd.Series([91,92,93]))
print('现有预测:')
print(predict_1)
print('预测未来3天:')
print(predict_2)
简化后
import pandas as pd
data = pd.read_csv('C:\\Users\\15614\\Desktop\\应用基础\\数模实战\\数据\\一手粉丝量 - 90 days.csv')
print(data)
import matplotlib.pyplot as plt
plt.scatter(data.time,data.fans)
plt.show()
x=data.time
y=data.fans
import tensorflow as tf
model=tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1,input_shape=(1,)))
model.compile(optimizer='adam',
loss='mse'
)
history=model.fit(x,y,epochs=5000)
model.predict(pd.Series([91,92,93]))
- 自己装好了,跳过。
My改进版
- 预测模型:y = w × x + b y=w\times x+ b y =w ×x +b
import random as rd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
eps=1e-6
np.random.seed(123)
x=np.random.random(100)*100
y=np.random.random(100)*340-666
x.sort()
y.sort()
plt.scatter(x, y)
w, b = 0, 0
num_epoch = 10000
learning_rate = 0.000005
loss=0
e=0
cnt_pass=0
while True :
e+=1
las_loss=loss
las_w=w
las_b=b
y_pred = w * x + b
loss = np.mean(np.square(y_pred-y))
grad_w, grad_b = (y_pred - y).dot(x), (y_pred - y).sum()
w, b = w - learning_rate * grad_w, b - learning_rate * grad_b
if abs(las_loss-loss)<eps and abs(las_w-w)<eps and abs(las_b-b)<eps :
cnt_pass+=1
if cnt_pass>100:
break
else:
cnt_pass=0
if e %2000 ==0:
plt.cla()
plt.scatter(x,y)
plt.plot(x, y_pred, 'r-', lw=5)
plt.title('Loss=%.4f | adj=%e | w=%.4f | b=%.4f'%(loss,learning_rate,w,b),
fontdict={'size': 14, 'color': 'red'})
plt.pause(0.1)
plt.cla()
plt.scatter(x,y)
plt.plot(x, y_pred, 'r-', lw=5)
plt.title('Done < at e=%d >\nLoss=%.4f | adj=%e | w=%.4f | b=%.4f'%(e,loss,learning_rate,w,b),
fontdict={'size': 14, 'color': 'red'})
plt.pause(0.1)
plt.show()
print(w, b)
乱搞的二次函数
- 预测模型:y = w × x 2 + b y=w\times x^2+ b y =w ×x 2 +b
import random as rd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
eps=1e-6
np.random.seed(123)
x=np.random.random(100)*100-50
x.sort()
y=np.array([x[i]**2+rd.random()*180. for i in range(100)])
plt.scatter(x, y)
xx=np.array([(x[i])**2*1. for i in range(100)])
w, b = 0, 0
num_epoch = 10000
learning_rate = 0.00000005
for e in range(num_epoch or True):
y_pred =w*xx+b
loss = np.mean(np.square(y_pred-y))
grad_w, grad_b = (y_pred - y).dot(x), (y_pred - y).sum()
las_w=w
las_b=b
w, b = w - learning_rate * grad_w, b - learning_rate * grad_b
if abs(las_w-w)<eps and abs(las_b-b)<eps :
break
if e %70 ==0:
plt.cla()
plt.scatter(x,y)
plt.plot(x, y_pred, 'r-', lw=5)
plt.title('Loss=%.4f | adj=%e | w=%.4f | b=%.4f'%(loss,learning_rate,w,b),
fontdict={'size': 14, 'color': 'red'})
plt.pause(0.1)
plt.cla()
plt.scatter(x,y)
plt.plot(x, y_pred, 'r-', lw=5)
plt.title('Done\nLoss=%.4f | adj=%e | w=%.4f | b=%.4f'%(loss,learning_rate,w,b),
fontdict={'size': 14, 'color': 'red'})
plt.pause(0.1)
plt.show()
print(w, b)
- 预测模型:y = w × ( x − 50 ) 2 + b y=w\times (x-50)^2+ b y =w ×(x −5 0 )2 +b
import random as rd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
eps=1e-6
np.random.seed(123)
x=np.random.random(100)*100
x.sort()
y=np.array([(x[i]-50)**2+rd.random()*180. for i in range(100)])
plt.scatter(x, y)
xx=np.array([(x[i]-50)**2*1. for i in range(100)])
w, b = 0, 0
num_epoch = 10000
learning_rate = 0.0000000005
for e in range(num_epoch or True):
y_pred =w*xx+b
loss = np.mean(np.square(y_pred-y))
grad_w, grad_b = (y_pred - y).dot(x), (y_pred - y).sum()
las_w=w
las_b=b
w, b = w - learning_rate * grad_w, b - learning_rate * grad_b
if abs(las_w-w)<eps and abs(las_b-b)<eps :
break
if e %70 ==0:
plt.cla()
plt.scatter(x,y)
plt.plot(x, y_pred, 'r-', lw=5)
plt.title('Loss=%.4f | adj=%e | w=%.4f | b=%.4f'%(loss,learning_rate,w,b),
fontdict={'size': 14, 'color': 'red'})
plt.pause(0.1)
plt.cla()
plt.scatter(x,y)
plt.plot(x, y_pred, 'r-', lw=5)
plt.title('Done\nLoss=%.4f | adj=%e | w=%.4f | b=%.4f'%(loss,learning_rate,w,b),
fontdict={'size': 14, 'color': 'red'})
plt.pause(0.1)
plt.show()
print(w, b)
Original: https://blog.csdn.net/qq_42710619/article/details/124246996
Author: 青菜 – Teloy_041
Title: 【一些笔记】TensorFlow笔记
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508823/
转载文章受原作者版权保护。转载请注明原作者出处!