Tensorflow学习
命名规范
矩阵变量 命名 首字母大写 如 Weights
常量 命名 正常 如 biases
tf.constant()
tensorflow 创建常量,即张量tensor
https://blog.csdn.net/csdn_jiayu/article/details/82155224
tensor=tf.constant([1, 2])
为查看结果必须创建一个会话,并用取值函数eval()来查看创建的tensor的值:
sess=tf.Session()
with sess.as_default():
print('结果是:', tensor.eval())
结果是:[1 2]
矩阵乘法
在numpy中, 是np.dot(m1,m2)
在tensorflow中,是tf.matmul(m1,m2) #matrix multiply
tf.matmul() 和tf.multiply() 的区别
tf.multiply()两个矩阵中对应元素各自相乘
tf.matmul()将矩阵a乘以矩阵b,生成a * b。
https://www.cnblogs.com/AlvinSui/p/8987707.html
在TensorFlow的世界里,变量的定义和初始化是分开的,所有关于图变量的赋值和计算都要通过tf.Session的run来进行。想要将所有图变量进行集体初始化时应该使用tf.global_variables_initializer。
Session 的两种打开方式
import tensorflow as tf
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],[2]])
product = tf.matmul(matrix1,matrix2)
### medthos 1
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close() #有无都可
### medthos 2
with tf.Session() as sess:
result2 = sess.run(product)
print(result2)
constant定义常量
Variable的使用
定义变量
用了Variable 必须要用tf.initialize_all_variables()
import tensorflow as tf
state = tf.Variable(0,name='counter')
#print(state.name)
one = tf.constant(1)
new_value = tf.add(state,one)
update = tf.assign(state,new_value) #将new_value的结果 更新到state上
init = tf.initialize_all_variables() #must have if define variable
with tf.Session() as sess:
sess.run(init) #session 中一定要run一次initialize
for _ in range(3):
sess.run(update)
print(sess.run(state))
Placeholder
当做占位符,在运行时输入数据
用了placeholder一定要用feed_dict 来喂入数据
Placeholder 中,规定shape大小 可以直接[2,2] 也可以shape=(2,2)
随机数据时 也是这样使用
import tensorflow as tf
import numpy as np
input1_data = np.random.randint(-2, 2, (2,2)).astype(np.float32) #定义随机数的shape时 要小括号(2,2)
input2_data = np.random.randint(-2, 2, (2,2)).astype(np.float32)
input1 = tf.placeholder(tf.float32,[2,2]) # placeholder 中定义占位符的shape大小 可直接[2,2] 也可以 shape=(2,2)
input2 = tf.placeholder(tf.float32,[2,2])
output = tf.matmul(input1,input2)
with tf.Session() as sess:
print(sess.run(output,feed_dict={input1:input1_data,input2:input2_data}))
x_data = np.linspace(-1, 1, 600)[:, np.newaxis]
https://blog.csdn.net/weixin_42866962/article/details/82811082
np.linspace 创建了-1到1之间的 600个数据
后面的写法 是为了增加1个维度 将一维的数据变为矩阵
[:,np.newaxis] 600行1列
[np.newaxis,:] 1行600列
noise = np.random.normal(0, 0.05, x_data.shape)
https://blog.csdn.net/qq_37701443/article/details/82797944
np.random.normal 正态分布
第一个参数loc (float)表示正态分布的均值,为0 说明 是以Y轴为对称轴的正态分布
第二个参数scale (float)表示正态分布的标准差,对应分布的宽度,scale越大,正态分布的曲线越矮胖,scale越小,曲线越高瘦。
第三个参数size(int 或者整数元组):输出的值赋在shape里,默认为None。
np.square(x_data)
x_data的平方
使用tf定义简单的神经网络
将其可视化
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# create dataset
x_data = np.linspace(-1, 1, 600)[:, np.newaxis] #在-1到1之间 创建600个数据,后面的写法是为了将一维的数据转变成一个矩阵 600行一列 如果[np.newaxis,:] 是1行600列
noise = np.random.normal(0, 0.05, x_data.shape) #正态分布 产生噪点 模拟数据 以Y轴为对称轴,scale=0.05 形状较高瘦 数据的shape和x_data.shape一样
y_data = np.square(x_data) -1.0 + noise #x_data的平方
#create placeholder tensor
xs = tf.placeholder(tf.float32, [None, 1]) #定义占位符 后面feed_dict 喂数据 dtype一定要有 shape 和 name 看情况
ys = tf.placeholder(tf.float32, [None, 1])
def add_layer(x, input_size, output_size, activation_func=None):
w = tf.Variable(tf.random_normal([input_size, output_size]))
b = tf.Variable(tf.zeros([output_size])+0.1)
out = tf.matmul(x, w) + b
if activation_func is None:
out = out
else:
out = activation_func(out)
return out
layer1 = add_layer(xs, 1, 20, activation_func=tf.nn.relu)
prediction = add_layer(layer1, 20, 1)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1])) #reduction_indices 坍塌维度 就是按照那个维度进行加法运算
train_op = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss) #设置学习率 按照减小的方向梯度下降
fig = plt.figure() #定义画布
ax = fig.add_subplot(1,1,1) #固定写法 或者fig.add_subplot(111)
ax.set_ylabel('y')
ax.set_xlabel('x')
ax.scatter(x_data, y_data) # 撒点
plt.ion() #变成交互模式 就是可以画动图
plt.show() #光用show()的话,就是画上线之后 会暂停主程序
with tf.Session() as sess:
tf.global_variables_initializer().run() #定义了变量需要 用这个
for i in range(2000):
_, los = sess.run([train_op, loss], feed_dict={xs: x_data, ys: y_data})
if i % 100:
data_pre = sess.run(prediction, feed_dict={xs: x_data}) #prediction 只与xs有关,所以只需要喂入xs的数据
try:
ax.lines.remove(lines[0]) # 每次优化后,抹除之前的线,新画一条线
except Exception:
pass
lines = ax.plot(x_data, data_pre, 'r-', lw=5) # plot表示曲线 输入数据 也就是 横坐标还是x_data 输出数据 即纵坐标就是 输出的预测值 r- 表示红色的线 lw表示线的宽度
plt.pause(0.1)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
得到的prediction为[600,1]的矩阵,先计算每个得分与样本的差距的平方,按列坍塌,再求和。最后求平均
reduction_indices 的意思
https://blog.csdn.net/qq_34062683/article/details/84847821