目录
(1)tf.compat.v1.placeholder ()
(3)tf.compat.v1.truncated_normal
(1)load_data()(tensorflow 2.0环境)
(2) read_data_sets()(tensorflow 1.0环境)
一,整体思路
暂时空缺
二,函数解析
1,数据计算部分
(1)tf.compat.v1.placeholder ()
函数原型:(function) placeholder: (dtype, shape=None, name=None) -> Any
作用:占位,创建一个空的张量,为数据计算预先留出空间。
参数:dtype:必选,张量内的数据类型,例如 tf.float32表示32位浮点型
shape:可选,指定张量的维度,例如[56,7]表示一个二阶张量,56行,7列
name:可选,这次操作的名字
注意:1,要想将空张量填充值,使用Session.run(), Tensor.eval(), or Operation.run().函数的feed_dict方法
2,直接使用会报错
运用示例:(官方代码)
import tensorflow as tf
x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.compat.v1.Session() as sess:
print(sess.run(y)) # ERROR: will fail because x was not fed.
rand_array = np.random.rand(1024, 1024)
print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.
(2)feed_dict方法
作用,将常量或空的张量替换,仅在调用它的语句内生效,在Session.run(), Tensor.eval(), or Operation.run().函数内使用
示例代码如下:
import tensorflow as tf
import numpy as np
tf.compat.v1.disable_eager_execution()
x = tf.constant([1,2,3],tf.float32)
z = tf.multiply(x,2)
y = tf.compat.v1.placeholder(tf.float32)
h = tf.multiply(y,2)
with tf.compat.v1.Session() as sess:
inin_op = tf.compat.v1.global_variables_initializer()
sess.run(inin_op)
#创造张量,值为随机数
temp = np.random.rand(2, 3)
c = np.random.rand(3,)
print(sess.run(h, feed_dict={y: temp,x:c}))
#将空张量y替换为temp,多个参数输入以','分割
#输出结果,[[0.5944809 0.3335819 0.41454604]
# [0.80106115 0.26287502 0.4975307 ]]
print(sess.run(z, feed_dict={x: c } ))
#将常数张量x替换为c
#输出结果, [0.1003684 1.0861734 0.3424338]
print(sess.run(z))
#由于feed_dict方法只在调用它的函数内生效,因此z的值不会被改变
#输出结果, [2. 4. 6.]
sess.close()
(3)tf.compat.v1.truncated_normal
原型:(function) truncated_normal: (shape, mean=0, stddev=1, dtype=dtypes.float32, seed=None, name=None) -> Any
shape表示生成张量的维度(a * a),mean是均值,stddev是标准差
此函数产生截断分布的正态函数,也就是产生的值如果与均值的差值大于两倍的标准差则丢弃重新选择,从而保证值都在(mean - 2*stddev,mean + 2*stddev)之内
(4)定义张量的函数
zeros() 定义一个张量,并将其所有值初始化为0
ones() 初始化值为1
constant() constant必须手动赋值,维度声明需要与值一致
Variable() 变量赋值与constant类似,也必须手动赋值,也可以调用以上函数,接收其他张量初始化
以下表达皆是合法的
a = tf.ones([3,1])
b = np.array([2,3,4])
b = tf.constant(b,shape=[3]) #shape可省略
c = tf.Variable(b)
d = tf.Variable(tf.zeros([2,7]))
(5)tf.matmul
矩阵相乘,形式c =tf.matmul(a,b),其张量a的分量必须能和b相乘,也就是b的维度为[3,4]的话
a的维度为[5,3],合法,产生张量的维度[5,4]
a的维度为[2,2,5,3],合法,产生张量的维度[2,2,5,4]
a的维度为[3,3],非法
a的维度为[2,4,3],非法,与b匹配的分量[4,3]不能和b相乘
(6)矩阵的行,列计算相关
reduce_sum: 计算和
reduce_mean 计算平均值
argmax argmin 寻找最大值,最小值,返回标签,以0开始
axis参数说明:指定计算的维度
默认:对所有数生效 0:以最高维度为对象 1:以次高维度为对象 .... x - 1:以最低维为对象
x为张量的维度
以reduce_sum为例,定义一个2*4*3的三维张量,分析输出的第一个结果
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
a = tf.constant([[[2,4,5],[4,6,7],[1,4,7],[7,4,9]],[[3,4,2],[1,3,7],[2,2,1],[8,7,9]]])
c_AXIS_None = tf.reduce_sum(a)
c_AXIS_0 = tf.reduce_sum(a,axis=0)
c_AXIS_1 = tf.reduce_sum(a,axis=1)
c_AXIS_2 = tf.reduce_sum(a,axis=2)
with tf.compat.v1.Session() as sess:
inin_op = tf.compat.v1.global_variables_initializer()
sess.run(inin_op)
print(sess.run(c_AXIS_None))
#109
print(sess.run(c_AXIS_0))
#[[ 5 8 7]
# [ 5 9 14]
# [ 3 6 8]
# [15 11 18]]
print(sess.run(c_AXIS_1))
#[[14 18 28]
# [14 16 19]]
print(sess.run(c_AXIS_2))
#[[11 17 12 20]
# [ 9 11 5 24]]
sess.close()
分析结果,第一个求出所有数的和
第二个,操作对象是最高维,此维度包含两个分量,因此
5(输出第一个结果) = 2(第一个分量的第一个数)+ 3(第二个分量的第一个数)
输出结果维度4*3,最高维度消失(输入是2*4*3)
第三个,操作对象是次高维,此维度包含四个分量,对象数量2,因此
对象1:[[2,4,5],[4,6,7],[1,4,7],[7,4,9]]
14(输出第一个结果) = 2(第一个分量的第一个数)+ 4(第二个分量的第一个数)+
1(第三个分量的第一个数)+ 7(第四个分量的第一个数)
输出结果维度2*3,次高维度消失(输入是2*4*3)
第四个,同样,对象是最低维度,对象共有2*4个
对第一个对象:[2,4,5],求和结果为11,输出结果维度为2*4(输入是2*4*3)
2,训练,测试数据获取,与预处理
(1)load_data()(tensorflow 2.0环境)
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train,训练使用图片数据,格式为3维张量,[60000*28*28],
y_train 训练使用图片数据的值,格式为2维张量,[60000*1],
x_test,y_test,同上,区别在于图片只有1万张
(2) read_data_sets()(tensorflow 1.0环境)
mnist = input_data.read_data_sets('data/',one_hot = True)
我们查看官方代码的源码部分,只关心返回值的形式
def fake():
return DataSet(
[], [], fake_data=True, one_hot=one_hot, dtype=dtype, seed=seed)
train = fake()
validation = fake()
test = fake()
return base.Datasets(train=train, validation=validation, test=test)
...
...
train = DataSet(train_images, train_labels, **options)
validation = DataSet(validation_images, validation_labels, **options)
test = DataSet(test_images, test_labels, **options)
return base.Datasets(train=train, validation=validation, test=test)
以mnist.train.images调出训练图片数据,其他依次类推
(3)其余函数
cast 数据强转
reshape 张量重新划分维度
one_hot 转换成独热码形式,例如2转成[0,0,1,0,0]
from_tensor_slices 张量组合
shuffle 打乱张量的排列
batch 将一定量的张量划分为一组,会增加一个维度
map() 对其中所有对象做一次括号内的操作
三,源代码
tensorflow 1.0环境
#导入相关库
import tensorflow as tf
from tensorflow_core.examples.tutorials import mnist
from tensorflow_core.examples.tutorials.mnist import input_data
from scipy import misc
import numpy as np
from PIL import Image
from itertools import chain
#下载MNIST手写数据集.将标签变换为10位宽独热码
mnist = input_data.read_data_sets('data/',one_hot = True)
#输入:x代表图片 y代表标签
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
Neure_Num = 64
#第一层网络 w1 b1
w1 = tf.Variable(tf.truncated_normal([784,Neure_Num],dtype = tf.float32))
b1 = tf.Variable(tf.zeros([1,Neure_Num],dtype = tf.float32))
#带入激活函数
y_1 = tf.nn.relu(tf.matmul(x,w1)+b1)
#第二层网络
w2 = tf.Variable(tf.ones([Neure_Num,10]),dtype = tf.float32)
b2 = tf.Variable(tf.zeros([1,10]),dtype = tf.float32)
#带入激活函数
y_2 = tf.nn.softmax(tf.matmul(y_1,w2)+b2)
#选择合适的损失函数
#一次推理出的10个结果中,取此次推理的正确结果的对应值,取对数再取负数,再将所有推理值求平均得损失值
loss = tf.reduce_mean(-tf.reduce_sum(y*tf.log(y_2),axis = 1))
#选择优化器(将损失函数带入优化器)
#梯度下降 AdamOptimizer() learning_rate = 0.0001(学习率)
train_setp = tf.train.AdamOptimizer(0.001).minimize(loss)
#计算正确个数 如果推理结果和标准结果y的最大值相等,此次推理是正确的
correct_prediction = tf.equal(tf.argmax(y_2, axis = 1),tf.argmax(y, axis = 1))
#计算正确率 强转为浮点型,计算平均值
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#初始化张量 初始化节点 (初始化之后才能使用)
init = tf.global_variables_initializer()
inin_op = tf.global_variables_initializer()
#打开会话
sess = tf.Session() #等价于 打开开关
sess.run(inin_op) #让数据先流过初始化节点
for i in range(10000):
#一次提取100个结果 由于mnist.train的数据格式
# train = DataSet(train_images, train_labels, **options)
# 因此batch_xs储存图片数据,bacth_ys储存图片标签
batch_xs,bacth_ys = mnist.train.next_batch(100)
#启动迅雷你
sess.run(train_setp,feed_dict = {x:batch_xs,y:bacth_ys})
if(i%100 == 0):
print("loss:",sess.run(loss,feed_dict = {x:mnist.test.images,y:mnist.test.labels}))
print("accuracy:",sess.run(accuracy,feed_dict = {x:mnist.test.images,y:mnist.test.labels}))