2018.12.4TensorFlow学习
01_tf常量变量
#opencv TensorFlow
#类比 语法 api 原理
#基本数据类型 运算符 流程 字典 数组
import tensorflow as tf
data1 = tf.constant(2,dtype=tf.int32) #定义常量
data2 = tf.Variable(10,name='val')
print(data1)
#Tensor("Const:0", shape=(), dtype=float32)
# Tensor:张量(一个数据) Const:常数 shape:维度 dtype:数据类型
print(data2)
'''
sess = tf.Session()
print(sess.run(data1))
# 常量需要初始化到session才可以使用
init = tf.global_variables_initializer() #实质也是一个计算图
sess.run(init)
print(sess.run(data2))
sess.close()
# 本质 tf = tensor+ 计算图
# tensor 数据
# op
# graohs 数据操作
# session
'''
init = tf.global_variables_initializer() #初始化
sess = tf.Session()
with sess:
sess.run(init)
print(sess.run(data2))
02_tf_四则运算
# 常量
'''
import tensorflow as tf
data1 = tf.constant(6)
data2 = tf.constant(2)
dataAdd = tf.add(data1,data2)# +
dataMul = tf.multiply(data1,data2)# *
dataSub = tf.subtract(data1,data2)# -
dataDiv = tf.divide(data1,data2)# /
with tf.Session() as sess:
print(sess.run(dataAdd))
print(sess.run(dataMul))
print(sess.run(dataSub))
print(sess.run(dataDiv))
print('end!')
'''
# 变量
import tensorflow as tf
data1 = tf.Variable(6)
data2 = tf.Variable(2)
dataAdd = tf.add(data1,data2)# +
dataCopy = tf.assign(data2,dataAdd)# dataAdd --> dataCopy
dataSub = tf.subtract(data1,data2)# -
dataMul = tf.multiply(data1,data2)# *
dataDiv = tf.divide(data1,data2)# /
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
# print(sess.run(data1))
# print(sess.run(data2))
print(sess.run(dataAdd))
print(sess.run(dataSub))
print(sess.run(dataMul))
print(sess.run(dataDiv))
print('sess.run(dataCopy)',sess.run(dataCopy))
print('dataCopy.eval()',dataCopy.eval())
print('tf.get_default_session.run(dataCopy)',tf.get_default_session().run(dataCopy))
print('end!')
03_tf矩阵基础1
# placehold
'''
import tensorflow as tf
data1 = tf.placeholder(tf.float32)
data2 = tf.placeholder(tf.float32)
dataAdd = tf.add(data1,data2)
with tf.Session() as sess:
print(sess.run(dataAdd,feed_dict = {data1:6,data2:2}))
# 1 dataAdd 2 data (feed_dice = {1:6,2:2}))
print('end!')
'''
# 类比 数组 M行N列 [] 内部[] [里面 列数据] []中括号整体行数
# [[6,6]] [[6,6]]
import tensorflow as tf
data1 = tf.constant([[6,6]])
data2 =tf.constant([[2],
[2]])
data3 = tf.constant([[3,3]])
data4 = tf.constant([[1,2],
[3,4],
[5,6]])
print(data4.shape)# 矩阵维度
with tf.Session() as sess:
print(sess.run(data4))# 打印整体
print(sess.run(data4[1])) # 打印某一列
print(sess.run(data4[:,0]))#MN 列 :表示所有
print(sess.run(data4[1,1]))#1 1
04_tf矩阵基础2
import tensorflow as tf
data1 = tf.constant([[6,6]])
data2 =tf.constant([[2],
[2]])
data3 = tf.constant([[3,3]])
data4 = tf.constant([[1,2],
[3,4],
[5,6]])
matMul = tf.matmul(data1,data2) #矩阵乘法
matMul2 = tf.multiply(data1,data2)#普通乘法
matAdd = tf.add(data1,data3)
with tf.Session() as sess:
print(sess.run(matMul))#1维 M=1 ,N=2 ,1x2(MK)2x1(KN)=
print(sess.run(matAdd)) #1行2列
print(sess.run(matMul2))#1x2 2x1 =2x2
print(sess.run([matMul,matAdd]))
05_tf矩阵基础3
'''
import tensorflow as tf
mat0 = tf.constant([[0,0,0],[0,0,0]])
mat1 = tf.zeros([2,3])
mat2 = tf.ones([3,2])
mat3 = tf.fill([2,3],15)
with tf.Session() as sess:
print(sess.run(mat0))
print(sess.run(mat1))
print(sess.run(mat2))
print(sess.run(mat3))
'''
import tensorflow as tf
mat1 = tf.constant([[2],[3],[4]])
mat2 = tf.zeros_like(mat1)
mat3 = tf.linspace(0.0,2.0,11)
mat4 = tf.random_uniform([2,3],-1,2)
with tf.Session() as sess:
print(sess.run(mat2))
print(sess.run(mat3))
print(sess.run(mat4))
06_tf模块Numpy的使用
# CRUD 增删改查
import numpy as np
data1 = np.array([1,2,3,4,5])
print(data1)
data2 = np.array([[1,2],
[4,5]])
print(data2)
print(data1.shape,data2.shape)# 维度
# zero ones
print(np.zeros([2,3]),'\n',np.ones([2,2]))
# 改查
data2[1,0] = 5
print(data2)
print(data2[1,1])
# 基本运算
data3 = np.ones([2,3])
print(data3*2)#对应相乘
print(data3/3)
print(data3+2)
#矩阵的 + -
data4 = np.array([[1,2,3],[4,5,6]])
print("data3+data4:\n",data3+data4)
print("data3*data4:\n",data3*data4)
07_tf模块matplotlib的使用
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7,8])
y = np.array([3,5,7,6,1,6,10,20])
plt.plot(x,y,'r')#绘制折线 1 x ,2 y , 3 color
plt.plot(x,y,'g',lw=10)# 4 line w
# 折线 饼状 柱状
x= np.array([1,2,3,4,5,6,7,8])
y = np.array([13,25,17,36,21,16,10,15])
plt.bar(x,y,0.3,alpha=0.3,color='b')# 柱状图 5 color,4 透明度 1不透 0.5半透,3 宽度
plt.show()
08神经网络逼近股票收盘价格
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
data = np.linspace(1,7,7)
# print(data)
endPrice = np.array([2511.90,2538.26,2510.68,2591.66,2732.98,2701.69,2714.89])
beginPrice = np.array([2438.90,2411.23,2573.77,2413.56,2512.33,2500.13,2713.59])
plt.figure()
for i in range(1,7):
#1 柱状图
dataOne = np.zeros([2])
dataOne[0] = i;
dataOne[1] = i;
priceOne = np.zeros([2])
priceOne[0] = beginPrice[i]
priceOne[1] = endPrice[i]
if endPrice[i]>beginPrice[i]:
plt.plot(dataOne,priceOne,'r',lw=8)
else:
plt.plot(dataOne,priceOne,'g',lw=8)
plt.show()
08 神经网络逼近股票收盘价格2
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
date = np.linspace(1,15,15)
endPrice = np.array([2511.90,2538.26,2510.68,2591.66,2732.98,2701.69,2701.29,2678.67,2726.50,2681.50,2739.17,2715.07,2823.58,2864.90,2919.08]
)
beginPrice = np.array([2438.71,2500.88,2534.95,2512.52,2594.04,2743.26,2697.47,2695.24,2678.23,2722.13,2674.93,2744.13,2717.46,2832.73,2877.40])
print(date)
plt.figure()
for i in range(0,15):
# 1 柱状图
dateOne = np.zeros([2])
dateOne[0] = i;
dateOne[1] = i;
priceOne = np.zeros([2])
priceOne[0] = beginPrice[i]
priceOne[1] = endPrice[i]
if endPrice[i]>beginPrice[i]:
plt.plot(dateOne,priceOne,'r',lw=8)
else:
plt.plot(dateOne,priceOne,'g',lw=8)
# plt.show()
# A输入层 , B隐藏层 ,C输出层
# w1 权重 , b1偏移量
# A(15*1)*w1(1*10)+b1(1*10) = B(15*10)
# B(15*10)*w2(10*1)+b2(15*1) =C(15*1)
# 1 A B C
dateNormal = np.zeros([15,1])
priceNormal = np.zeros([15,1])
for i in range(0,15):
dateNormal[i,0] = i/14.0;
priceNormal[i,0] = endPrice[i]/3000.0
x=tf.placeholder(tf.float32,[None,1])
y=tf.placeholder(tf.float32,[None,1])
#B
w1 = tf.Variable(tf.random_uniform([1,10],0,1))
b1 = tf.Variable(tf.zeros([1,10]))
wb1 = tf.matmul(x,w1)+b1
layer1 = tf.nn.relu(wb1)# 激励函数
#C
w2 = tf.Variable(tf.random_uniform([10,1],0,1))
b2 = tf.Variable(tf.zeros([15,1]))
wb2 = tf.matmul(layer1,w2)+b2
layer2 = tf.nn.relu(wb2)
loss = tf.reduce_mean(tf.square(y-layer2))#reduce_mean 计算差异(标准差),square 开方,y 真实值 ,layer2 计算值
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) #梯度下降法
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(0,10000):
sess.run(train_step,feed_dict={x:dateNormal,y:priceNormal})
pred = sess.run(layer2,feed_dict={x:dateNormal})
predPrice = np.zeros([15,1])
for i in range(0,15):
predPrice[i,0] = (pred*3000)[i,0]
plt.plot(date,predPrice,'b',lw=1)
plt.show()
总结
经过这一天的学习,
- 总体上来说对TensorFlow有了一些了解
- 最后的案例有点迷糊
- 继续努力