6.1 经典的MINST数据集
##tensorflow1.0 load mnist dataset
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST_data",one_hot=Ture)
print("Traing daya anf label size:")
print(mnist.train.images.shape,mnist.train.labels.shape)
print("Test data and label size:")
print(mnist.test.images.shape,mnist.test.labels.shape)
print("Validating data and label size:")
print(mnist.validation.images.shape,mnist.validation.labels.shape)
print("Example traing data :",mnist.train.images[0])
print("Example traing label:",mnist.train.labels[0])
##tensorflow2.0 load mnist dataset
import tensorflow as tf
(train_images,train_labels),(test_images,test_labels)=tf.keras.dataset.mnist.load_data()
print("train_images'shape is:",train_images.shape)
print("train_labels'shape is :",train_labels.shape)
print("test_images'shape is: ",test_images.shape)
print("test_labels'shape is :",tset_labels.shape)
print(train_images[0])
print(train_labels[0])
print(test_labels[0])
6.2 Fashion-MINST数据集
import tensorflow as tf
import matplotlib.pyplot as plt
(train_images,train_labels),(test_images,test_labels)=tf.keras.dataset.fashion_mnist.load_data()
print("train_images'shape is:",train_images.shape)
print("train_labels'shape is :",train_labels.shape)
print("test_images'shape is: ",test_images.shape)
print("test_labels'shape is :",tset_labels.shape)
plt.figure()
plt.imshow(train_images[0],cmap=plt.cm.binary)
plt.colorbar()
plt.grid(False)
plt.show()
查看数据集中隐藏文件夹:nautilus home/xxy/.keras/datasets/
6.3 最直接的网络构建方式
import tensorflow as tf
import numpy as np
TOTAL_EPOCHS=20
(train_images,train_labels),(test_images,test_labels)=tf.keras.datasets.mnist.load_data()
model=tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=[28,28]))
model.add(tf.keras.layers.Dense(500,activation='relu'))
model.add(tf.keras.layers.Dense(10,activation='softmax'))
model.complie(optimizer='SGD',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
for i in range(TOTAL_EPOCHS):
model.fit(x=train_images,y=train_labels,batch_size=100,epochs=10,initial_epoch=0)
print("the result of test module : ")
model.evaluate(test_images,test_labels)
class_names=['0','1','2','3','4','5','6','7','8','9']
predictions=model.predict(test_images)
correct_predicition=tf.equal(np.argmax(predictions,1),test_labels)
predict_accuracy=tf.reduce_mean(tf.cast(correct_predicition,tf.float32))
print("predict accuracy is : ",predict_accuracy.numpy())
for i in range(100):
print("predict reslut: ",class_names[np.argmax(predictions[i])])
print("correct clarified result is : ",class_names[test_labels[i]])
6.4超参数和验证集
6.5从头编写层和模型
import tensorflow as tf
from tensorflow.keras import layers
class Sum(layers.Layer):
def __init__(self,input_dim):
super(Sum,self).__init__()
self.var=tf.Variable(initial_value=tf.zeros((input_dim,)),trainable=False)
def call(self,inputs):
self.var.assign_add(tf.reduce_sum(inputs,axis=0))
return self.var
x=tf.ones((2,2))
sum=Sum(2)
y=sum(x)
print(y.numpy())
y=sum(x)
print(y.numpy())
import tensorflow as tf
from tensorflow.keras import layers
class Sum(layers.Layer):
def __init__(self,input_dim):
super(Sum,self).__init__()
self.var=tf.Variable(initial_value=tf.zeros((input_dim,)),trainable=False)
def call(self,inputs):
self.var.assign_add(tf.reduce_sum(inputs,axis=0))
return self.var
x=tf.ones((2,2))
sum=Sum(2)
y=sum(x)
print(y.numpy())
y=sum(x)
print(y.numpy())
##Fasion_MINIST datasets
import tensorflow as tf
from tensorflow.keras import layers
(train_images,train_labels),(test_images,test_labels)=tf.keras.datasets.Fasion_MINIST.load_data()
train_images=train_images.reshape(60000,784).astype('float32')/255
test_images=test_images.reshape(10000,784).astype('float32')/255
##create data bath
train_dataset=tf.data.Dataset.from_tensor_slices((train_images,train_labels))
train_dataset=train_dataset.shuffle(buffer_size=1024).batch(100)
test_dataset=tf.data.Dataset.from_tensor_slices((test_images,test_labels))
test_dataset=test_dataset.batch(100)
class InputLayers(layers.Layer):
def __init__(self,uints=784):
super(InputLayer,self).__init__()
self.units=units
def call(self,inputs):
return inputs
class HiddenLayer(layers.Layer):
def __init__(self,uints):
super(HiddenLayer,self).__init__()
self.units=units
def build(self,input_shape):
w_init=tf.radom_normal_initializer()
self.w=tf.Variable(initial_value=w_init(shape=(input_shape[-1],self.units),dtype='float32'),trainable=True)
b_init=tf.zeros_initializer()
self.b=tf.Variable(initial_value=b_init(shape=(slef.units,),dtype='float32'),trainable=True)
'''
add_weight(name=None,shape=None,dtype=None,initializer=None,
regularizer=None,trainable=None,constraint=None,partitioner=None,
use_resource=None,synchronization=tf.VariableSynchronization.AUTO,
aggregation=tf.compat.v1.VariableAggregation.NONE,**kwargs)
'''
def call(self,inputs):
output=tf.matmul(inputs,self.w)+self.b
regularizer_l2=tf.keras.regularizer.l2.(.01)
self.add_loss(regularizer_l2(slef.w))
return output
class OutputLayer(layers.Layer):
def __init__(slef,units):
super(OutputLayer,self).__init__()
self.units=units
def build(self,input_shape):
w_init=tf.random_nromal_initializer()
self.w=tf.Variable(init_value=w_init(shape=(input_shape[-1],self.units),dtype='float32'),trainable=True)
def call(self,inputs):
output=tf.matmul(inputs,self.w)+self.b
regularizer_l2=tf.keras.regularizers.l2(.001)
self.add_loss(regularizer_l2(self.w))
return output
class MPLBlock(tf.keras.Model):
def __init__(self):
super(MLPBlock,self).__init__()
self.hidden_layer=InputLayer(784)
self.output_layer=OutputLayer(10)
self.softmax=layers.Softmax()
def call(self,input):
x=self.input_layer(input)
x=self.hidden_layer(x)
x=tf.nn.relu(x)
x=self.output_layer(x)
x=self.softmax(x)
return x
loss_fn=tf.keras.SparseCategoricalCrossentropy()
optimizer=tf.keras.optimizers.Adam()
train_loss=tf.keras.metrics.Mean(name='train_loss')
train_accuracy=tf.keras.metrics.SparseCategoricalAccuary(name='train_accuracy')
test_loss=tf.keras.metrics.Mean(name='test_loss')
test_accuracy=tf.keras.metrics.SparseCategoricalAccuary(name='test_accuracy')
mlpmodel=MLPBlock()
@tf.function
def train_step(train_images,train_labels):
with tf.GradientTape() as tape:
predictions=mlpmodel(train_images)
loss=loss_fn(train_labels,predictions)
total_loss=loss+sum(mlpmodel.losses)
gradients=tape.gradient(total_loss,mlpmodel.trainable_variables)
optimizer.apply_gradients(zip(gradients,mlpmodel.trainable_variables))
train_loss(loss)
train_accuracy(train_labels,predictions)