Keras基础
keras基于tensorflow库,在python中使用,暂时为安装GPU,训练速度可能有些慢。
步骤:
1.导入相应库
2.收集数据
3.处理数据,划分测试集和训练集
4.构建model,模块化构建
5.设置loss,optimizer 损失函数 ,优化器
6.进行训练
7.进行测试
8.结果的可视化
1.tensorflow基本内容
构建图->运行图
使用tensorlfow2.x
1.忽略waring以下的问题
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
2.运行tensorflow1.x的代码
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
#或
#import tensorflow.compat.v1 as tf
#tf.disable_v2_behavior()
#使用时
tf.compat.v1...............
3.构件图
常量 tf.constant(2)
变量 a=tf.Varibale(initial_value=2) #初始化
init=tf.compat.v1.global_variables_initializer()
展位符 x=tf.compat.v1.placeholder(tf.float32)
4.运行图
with tf.compat.v1.Session() as sess:
sess.run(init)
print(sess.run(c,feed_dict={a:3,b:4}))
5.tensorflow API
-
tf.app 为tensorflow脚本提供main函数
-
tf.image 图像处理,颜色变化,图像编码解码
-
tf.gfile 文件操作函数
-
summary 生成tensorboard可用统计日志
-
tf.python_io 读写TFRecord文件
-
tf.train 提供一些训练器
-
tf.nn 构建神经网络底层函数 ,卷积池化等
6.线性回归实例
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
#data
x=tf.compat.v1.random_normal(shape=[100,1])
y=tf.matmul(x,[[0.8]])+0.7
w=tf.Variable(initial_value=tf.compat.v1.random_normal(shape=[1,1]))
b=tf.Variable(initial_value=tf.compat.v1.random_normal(shape=[1,1]))
y_pre=tf.matmul(x,w)+b
#loss
loss=tf.reduce_mean(tf.square(y_pre-y))
#optimizer
optimizer=tf.compat.v1.train.GradientDescentOptimizer(learning_rate=0.05).minimize((loss))
#收集变量
tf.compat.v1.summary.scalar("loss",loss)
tf.compat.v1.summary.histogram("weight",w)
tf.compat.v1.summary.histogram("bias",b)
merged=tf.compat.v1.summary.merge_all()
#变量初始化
init=tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
sess.run(init)
filt_writer=tf.compat.v1.summary.FileWriter("F:\PyCharm Community Edition 2020.3.2\DeepLearning")
print("训练前的模型参数为:权重:%f,偏置:%f,损失为:%f" % (w.eval(), b.eval(),loss.eval()))
for i in range(201):
sess.run(optimizer)
if(i%20==0):
print("第%d次训练后模型参数,权重:%f,偏置:%f,损失:%f" %(i+1,w.eval(),b.eval(),loss.eval()))
summary=sess.run(merged)
filt_writer.add_summary(summary,i)
7.模型保存
tf.train.Saver(var_list=None,max_to_keep=5)
# 创建saver对象
saver = tf.compat.v1.train.Saver(max_to_keep=5)
if os.path.exists("D:/AliyunEDU/04 model/lr_model.ckpt"):
saver.restore(sess,"D:/AliyunEDU/04 model/checkpoint")
8.tensorboard可视化
保存的logs的path
在cmd下 tensorboard --logdir=“path”
9.图片数据的输入
数据cifar 使用队列读入二进制文件,把样本特征通过example写入文件
再通过文件名队列,解码读取TFRecord文件并解析
图片数据读入和解码
import os
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
def picture_read():
filename=os.listdir("E:/Picture/animation")
#拼接路径名和文件名,路径使用/
file_list=[os.path.join("E:/Picture/animation/",file) for file in filename]
#加入文件队列
file_queue=tf.compat.v1.train.string_input_producer(file_list)
#读取文件
reader=tf.compat.v1.WholeFileReader()
key,value=reader.read(file_queue)
#照片解码
img=tf.compat.v1.image.decode_jpeg(value)
#重新设置大小,和shape
img=tf.compat.v1.image.resize(img,[200,200])
img.set_shape(shape=[200,200,3])
#设置batch
img_batch=tf.compat.v1.train.batch([img],batch_size=100,num_threads=1,capacity=100)
with tf.compat.v1.Session() as sess:
#创建线程
coord=tf.compat.v1.train.Coordinator()
thread=tf.compat.v1.train.start_queue_runners(sess=sess)
key_n,value_n,img_resized_n,img_batch_n=sess.run([key,value,img,img_batch])
print(img_resized_n)
#回收线程
coord.request_stop()
coord.join(thread)
picture_read()
10.TFRecords文件
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
import os
class Cifar(object):
def __init__(self):
# 初始化操作
self.height = 32
self.width = 32
self.channels = 3
# 字节数
self.image_bytes = self.height * self.width * self.channels
self.label_bytes = 1
self.all_bytes = self.label_bytes + self.image_bytes
def read_binary(self):
# 1.构造文件名队列
file_name = os.listdir("D:/Project/Data/cifar-10-bin")
# 构建文件名路径列表
file_list = [os.path.join("D:/Project/Data/cifar-10-bin/", file) for file in file_name if file[-3:] == "bin"]
file_queue = tf.compat.v1.train.string_input_producer(file_list)
# 2.读取与解码
## 读取阶段
reader = tf.compat.v1.FixedLengthRecordReader(self.all_bytes)
key,value = reader.read(file_queue)
print("key:\n{}\n value:\n{}\n".format(key,value))
## 解码阶段
decoded = tf.compat.v1.decode_raw(value, tf.uint8)
## 1_将目标值和特征值切片切开
label = tf.slice(decoded, [0], [self.label_bytes])
image = tf.slice(decoded, [1], [self.image_bytes])
## 2_调整图片形状,以符合tensor的输入要求
image_reshaped = tf.reshape(image, shape=[self.channels,self.height,self.width])
## 3_转置 将图片调整为 HWC
image_transposed = tf.transpose(image_reshaped,[1,2,0])
print("image_reshaped:{}\n image_tansposed:{}\n".format(image_reshaped,image_transposed))
## 4_调整图像类型 uint8->float32
image_cast = tf.cast(image_transposed, tf.float32)
# 3.批处理
label_batch,image_batch = tf.compat.v1.train.batch([label,image_cast],batch_size=100,num_threads=1,capacity=100)
print(label_batch)
# 开启会话
with tf.compat.v1.Session() as sess:
# 开启线程
coord = tf.compat.v1.train.Coordinator()
threads = tf.compat.v1.train.start_queue_runners(sess=sess, coord=coord)
key_new, value_new, decoded_new, label_new, image_new,image_reshaped_new,image_transposed_new = sess.run(
[key, value, decoded, label, image, image_reshaped, image_transposed]
)
label_value, image_value = sess.run([label_batch, image_batch])
print("decoded_new:\n",decoded_new)
# 回收线程
coord.request_stop()
coord.join(threads)
return image_value, label_value
def write_to_tfrecords(self, image_batch, label_batch):
"""
将样本的特征值写入TFRecords文件
"""
with tf.compat.v1.python_io.TFRecordWriter("cifar10.tfrecords") as writer:
# 循环构造example对象,并序列化写入文件
for i in range(100):
image = image_batch[i].tostring() #bytes类型
label = label_batch[i][0] #取出整型单值
#print("label:{}\n image:{}".format(label,image))
example = tf.compat.v1.train.Example(features=tf.compat.v1.train.Features(feature={
"image":tf.compat.v1.train.Feature(bytes_list=tf.compat.v1.train.BytesList(value=[image])),
"label":tf.compat.v1.train.Feature(int64_list=tf.compat.v1.train.Int64List(value=[label])),
}))
#example.SerializeToString()
# 将序列化后的example下入example文件
writer.write(example.SerializeToString())
return None
def read_tfrecords(self):
# 1.构造文件名队列
file_queue = tf.compat.v1.train.string_input_producer(["cifar10.tfrecords"])
# 2.读取与解码
reader = tf.compat.v1.TFRecordReader()
key,value = reader.read(file_queue)
# 解析 example
feature = tf.compat.v1.parse_single_example(value, features={
"image":tf.compat.v1.FixedLenFeature([], tf.string),
"label":tf.compat.v1.FixedLenFeature([], tf.int64)
})
image = feature["image"]
label = feature["label"]
## 解码
image_decoded = tf.compat.v1.decode_raw(image, tf.uint8)
print("image_decoded:",image_decoded)
## 图像形状调整
image_reshaped = tf.compat.v1.reshape(image_decoded, [self.height, self.width, self.channels])
# 3.构造批处理队列
image_batch,label_batch = tf.compat.v1.train.batch([image_reshaped, label], batch_size=100, num_threads=2, capacity=100)
with tf.compat.v1.Session() as sess:
# 开启线程
coord = tf.compat.v1.train.Coordinator()
threads = tf.compat.v1.train.start_queue_runners(sess=sess, coord=coord)
image_value, label_value = sess.run([image_batch, label_batch])
print("image_value:\n",image_value)
# 回收线程
coord.requset_stop()
coord.join(threads)
return None
# 实例化Cifar
cifar = Cifar()
image_value, label_value = cifar.read_binary()
cifar.write_to_tfrecords(image_value, label_value)
2.单层线性模型
#Regression 单层全连接网络,线性回归模型
import numpy as np
import matplotlib.pylab as plt
np.random.seed(3)
from keras.models import Sequential
from keras.layers import Dense
#数据
X=np.linspace(-1,1,200)
np.random.shuffle(X) #将数据随机化
Y=0.5 *X +2+np.random.normal(0,0.05,(200,))
X_train,Y_train=X[:160],Y[:160]
X_test,Y_test=X[160:],Y[160:]
#创建模型
model=Sequential()
model.add(Dense(units=1))#输入维度 1,输出维度 1
#选择损失函数,均方差loss,随机梯度下降优化
model.compile(loss='mse',optimizer='sgd')
print("training------------------------")
for step in range(301):
cost =model.train_on_batch(X_train,Y_train) #训练一轮
if step %1000==0:
print("train cost",cost)
print("Testing------------------------")
cost=model.evaluate(X_test,Y_test,batch_size=4)
W,b=model.layers[0].get_weights()
#plt
Y_pred=model.predict(X_test)
plt.scatter(X_test,Y_test)
plt.plot(X_test,Y_pred)
plt.show()