import tensorflow as tf
# tensorflow实质张量tensor+计算图grahps
# 常量
hello = tf.constant("hello world22")
# 变量
data=tf.Variable(10,name='var')
data1=tf.constant(11,dtype=tf.int16,name='var')
print(data)
print(data1)
print(hello)
'''
sess = tf.Session()
print(sess.run(data1))
init =tf.global_variables_initializer()
sess.run(init)
print(sess.run(hello))
print(sess.run(data))
sess.close()
'''
# 使用with可以不用关闭
init=tf.global_variables_initializer()
sess=tf.Session()
with sess:
sess.run(init)
print(sess.run(data))
import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\1.jpeg",1)
# cv2.imwrite(r"C:\Users\Administrator\Desktop\222\2.jpeg",img,[cv2.IMWRITE_JPEG_QUALITY,10])
(b,g,r)=img[100,100]
print(b,g,r)
for i in range(100):
img[10+i,100]=(255,0,0)
cv2.imshow('image',img)
cv2.waitKey(0)
123 137 133
import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\1.jpeg",1)
# cv2.imshow('image',img)
# cv2.waitKey(0)
# png 无损压缩 0-9 0压缩比最小 jpg0-100 0压缩比最大
cv2.imwrite(r"C:\Users\Administrator\Desktop\222\2.png",img,[cv2.IMWRITE_PNG_COMPRESSION,9])
1 常量四则运算
# 常量四则运算
import tensorflow as tf
data1 =tf.constant(6)
data2 =tf.constant(2)
dataAdd=tf.add(data1,data2)
dataSub=tf.subtract(data1,data2)
dataMul=tf.multiply(data1,data2)
dataDiv=tf.divide(data1,data2)
with tf.Session() as sess:
print(sess.run(dataAdd))
print(sess.run(dataSub))
print(sess.run(dataMul))
print(sess.run(dataDiv))
8
4
12
3.0
2 变量四则运算
# 变量四则运算
import tensorflow as tf
data1 =tf.Variable(6)
data2 =tf.constant(2)
dataAdd=tf.add(data1,data2)
dataCopy=tf.assign(data1,dataAdd)#dataAdd->data1 注意data1必须是变量
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:
print(sess.run(init))
print(sess.run(dataAdd))
print(sess.run(dataSub))
print(sess.run(dataMul))
print(sess.run(dataDiv))
print(sess.run(dataCopy))
print("dataCopy.eval()",dataCopy.eval())#会重新计算此时data1为8 data1+data2=10 10->dataCopy
print("dataCopy.eval()",tf.get_default_session().run(dataCopy))#会重新计算此时data1为10 data1+data2=12 12->dataCopy
None
8
4
12
3.0
8
dataCopy.eval() 10
dataCopy.eval() 12
#矩阵基础
#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:4,data2:11}))
15.0
# 矩阵
import tensorflow as tf
data1 = tf.constant([[6,7]])
data2 = tf.constant([[2],[3]])
data3 = tf.constant([[3,4]])
data4 = tf.constant([[3,4],[5,6],[7,8]])
print(data4.shape)#打印矩阵的纬度
with tf.Session() as sess:
print(sess.run(data4))
print(sess.run(data4[0])) #打印某一行
print(sess.run(data4[:,1])) #打印某一列
print(sess.run(data4[0][1]))
(3, 2)
[[3 4]
[5 6]
[7 8]]
[3 4]
[4 6 8]
4
# 矩阵运算
import tensorflow as tf
data1 = tf.constant([[6,7]])
data2 = tf.constant([[2],
[3]])
data3 = tf.constant([[3,4]])
data4 = tf.constant([[3,4],
[5,6],
[7,8]])
data5 = tf.constant([[2,3]])
matMul = tf.matmul(data1,data2)#矩阵相乘
matMul2 = tf.multiply(data1,data2)#矩阵的对应元素相乘
matMul3 = tf.multiply(data1,data5)#矩阵的对应元素相乘
matAdd = tf.add(data1,data3)
with tf.Session() as sess:
print(sess.run(matMul))
print(sess.run(matAdd))
print(sess.run(matMul2))
print(sess.run(matMul3))
# print(sess.run([matMul,matMul2]))
[[33]]
[[ 9 11]]
[[12 14]
[18 21]]
[[12 21]]
全零矩阵和全1矩阵,随机矩阵,及填充
import tensorflow as tf
data1 = tf.constant([[0,0,0],[0,0,0]])
mat1 = tf.zeros([2,3])
mat2 = tf.ones([2,3])
mat3 = tf.fill([2,3],6)
mat4 = tf.zeros_like(mat3)
mat5 = tf.linspace(0.0,2.0,11)#从0到2 分成11个元素
mat6 = tf.random_uniform([2,3],-2,2)#随机矩阵,元素在-2到2之间
with tf.Session() as sess:
print(sess.run(data1))
print(sess.run(mat1))
print(sess.run(mat2))
print(sess.run(mat3))
print(sess.run(mat4))
print(sess.run(mat5))
print(sess.run(mat6))
[[0 0 0]
[0 0 0]]
[[0. 0. 0.]
[0. 0. 0.]]
[[1. 1. 1.]
[1. 1. 1.]]
[[6 6 6]
[6 6 6]]
[[0 0 0]
[0 0 0]]
[0. 0.2 0.4 0.6 0.8 1. 1.2
1.4 1.6 1.8000001 2. ]
[[ 0.20570135 -1.4024053 -0.2624688 ]
[ 1.0370159 0.9554014 -0.68394613]]
import numpy as np
data1 = np.array([1,2,3,4,5])
print(data1)
data2 = np.array([[1,2,3,4,5],[1,2,3,4,5]])
print(data2)
print(data1.shape,data2.shape)#打印纬度
#zero和one
print(np.zeros([2,3]),np.ones([2,2]))
#改查
data2[0,0]=0
print(data2)
print(data2[1,1])
#基本运算
data3 = np.ones([2,3])
print(data3*4)#加减乘除操作类比
#矩阵的加减乘除
data4 = np.array([[1,2,3],[4,5,6]])
print(data3+data4)#对应元素相加
print(data3*data4)#对应元素相乘
print(data3*data4+np.array([2,3,4]))#对应元素相乘
[1 2 3 4 5]
[[1 2 3 4 5]
[1 2 3 4 5]]
(5,) (2, 5)
[[0. 0. 0.]
[0. 0. 0.]] [[1. 1.]
[1. 1.]]
[[0 2 3 4 5]
[1 2 3 4 5]]
2
[[4. 4. 4.]
[4. 4. 4.]]
[[2. 3. 4.]
[5. 6. 7.]]
[[1. 2. 3.]
[4. 5. 6.]]
[[ 3. 5. 7.]
[ 6. 8. 10.]]
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7,8])
y = np.array([8,5,6,4,5,6,7,8])
plt.plot(x,y,'r')
plt.plot(x+2,y,'b')
#折现 饼状 柱状
plt.bar(x,y,0.5,alpha=1,color='g') #alpha 为透明度, 第二个参数为柱状图的宽度
plt.show()
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
date =np.linspace(1,15,15)
date1=np.array([[1,2,3],[4,5,6]])
print(date.shape)
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])
plt.figure()
for i in range(0,15):
#1柱状图
dataOne = np.zeros([2])
dataOne[0]=i+1
dataOne[1]=i+1
priceOne=np.zeros([2])
priceOne[0]=beginPrice[i]
priceOne[1]=endPrice[i]
# print(dataOne,priceOne)
if endPrice[i]>beginPrice[i]:
plt.plot(dataOne,priceOne,'r',lw=8)
else:
plt.plot(dataOne,priceOne,'g',lw=8)
# plt.plot(date,beginPrice,'y')
# plt.plot(date,endPrice,'g')
# A(15x1)*w1(1x10)+b1(1*10) = B(15x10)
# B(15x10)*w2(10x1)+b2(15x1) = C(15x1)
# 1 A B C \n"
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),name="w1")
b1 = tf.Variable(tf.zeros([1,10]),name="b1")
wb1 = tf.matmul(x,w1)+b1
layer1 = tf.nn.relu(wb1)
#C
w2 = tf.Variable(tf.random_uniform([10,1],0,1),name="w2")
b2 = tf.Variable(tf.zeros([15,1]),name="b2")
wb2 = tf.matmul(layer1,w2)+b2
layer2 = tf.nn.relu(wb2)
loss = tf.reduce_mean(tf.square(y-layer2))#y 真实 layer2 计算
train_step =tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# print(tf.trainable_variables())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())#初始化全部变量
for i in range(0,1000):
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()