tensorflow:自定义卷积层实现MNIST识别

新书开不停,只为混脸熟~

一,代码

import numpy as np
import tensorflow as tf
from tensorflow import keras


"""

labels = []
vocab = set()
context = []

with open('ChnSentiCorp.txt', mode='r', encoding='UTF-8') as emotion_file:
    for line in emotion_file.readlines():
        line = line.strip().split(',')
        labels.append(int(line[0]))

        text = line[1]
        context.append(text)
        for char in text: vocab.add(char)

vocab_list = list(sorted(vocab))
token_list = []
for text in context:
    token = [vocab_list.index(char) for char in text]
    token = token[:80] + [0] * (80 - len(token))
    token_list.append(token)
token_list = np.array(token_list)
labels = np.array(labels)
input_token = tf.keras.Input(shape=(80,))
embedding = tf.keras.layers.Embedding(input_dim=3508, output_dim=128)(input_token)
embedding = tf.keras.layers.Bidirectional(tf.keras.layers.GRU(128))(embedding)
output = tf.keras.layers.Dense(2, activation=tf.nn.softmax)(embedding)
model = tf.keras.Model(input_token, output)

model.compile(optimizer='adam', loss=tf.keras.losses.sparse_categorical_crossentropy, metrics=['accuracy'])
model.fit(token_list, labels, epochs=10, verbose=2)

input = tf.Variable(tf.random.normal([1, 5, 5, 1]))
conv = tf.keras.layers.Conv2D(1, 2, strides=[2, 2], padding='SAME')(input)
print(conv.shape)
"""

mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = tf.expand_dims(x_train, -1)
y_train = np.float32(tf.keras.utils.to_categorical(y_train, num_classes=10))
x_test = tf.expand_dims(x_test, -1)
y_test = np.float32(tf.keras.utils.to_categorical(y_test, num_classes=10))
batch_size = 512
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(batch_size).shuffle(batch_size * 10)
test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(batch_size)


class MyLayer(tf.keras.layers.Layer):
    def __init__(self, kernel_size, filter):
        self.filter = filter
        self.kernel_size = kernel_size
        super().__init__()

    def build(self, input_shape):
        self.weight = tf.Variable(tf.random.normal([self.kernel_size, self.kernel_size, input_shape[-1], self.filter]))
        self.bias = tf.Variable(tf.random.normal([self.filter]))
        super().build(input_shape)

    def call(self, input_tensor):
        conv = tf.nn.conv2d(input_tensor, self.weight, strides=[1, 2, 2, 1], padding='SAME')
        conv = tf.nn.bias_add(conv, self.bias)
        out = tf.nn.relu(conv) + conv
        return out


input_xs = tf.keras.Input([28, 28, 1])
conv = tf.keras.layers.Conv2D(32, 3, padding='SAME', activation=tf.nn.relu)(input_xs)
conv = MyLayer(32, 3)(conv)
conv = tf.keras.layers.BatchNormalization()(conv)
conv = tf.keras.layers.Conv2D(64, 3, padding='SAME', activation=tf.nn.relu)(conv)
conv = tf.keras.layers.MaxPool2D(strides=[1, 1])(conv)
conv = tf.keras.layers.Conv2D(128, 3, padding='SAME', activation=tf.nn.relu)(conv)
flat = tf.keras.layers.Flatten()(conv)
dense = tf.keras.layers.Dense(512, activation=tf.nn.relu)(flat)
logits = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(dense)
model = tf.keras.Model(inputs = input_xs, outputs=logits)
print(model.summary())

model.compile(optimizer=tf.optimizers.Adam(1e-3), loss=tf.losses.categorical_crossentropy, metrics=['accuracy'])
model.fit(train_dataset, epochs=10)
model.save('model.h5')
score = model.evaluate(test_dataset)
print('last score:',score)


二,输出

C:\Users\ccc\AppData\Local\Programs\Python\Python38\python.exe D:/tmp/tup_ai/codes/2.clustering/kmeans/srcnn.py
2022-06-30 23:37:06.788702: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 28, 28, 1)]       0         
                                                                 
 conv2d (Conv2D)             (None, 28, 28, 32)        320       
                                                                 
 my_layer (MyLayer)          (None, 14, 14, 3)         98307     
                                                                 
 batch_normalization (BatchN  (None, 14, 14, 3)        12        
 ormalization)                                                   
                                                                 
 conv2d_1 (Conv2D)           (None, 14, 14, 64)        1792      
                                                                 
 max_pooling2d (MaxPooling2D  (None, 13, 13, 64)       0         
 )                                                               
                                                                 
 conv2d_2 (Conv2D)           (None, 13, 13, 128)       73856     
                                                                 
 flatten (Flatten)           (None, 21632)             0         
                                                                 
 dense (Dense)               (None, 512)               11076096  
                                                                 
 dense_1 (Dense)             (None, 10)                5130      
                                                                 
=================================================================
Total params: 11,255,513
Trainable params: 11,255,507
Non-trainable params: 6
_________________________________________________________________
None
Epoch 1/10
118/118 [==============================] - 30732s 263s/step - loss: 0.4375 - accuracy: 0.8774
Epoch 2/10
118/118 [==============================] - 746s 6s/step - loss: 0.0824 - accuracy: 0.9745
Epoch 3/10
 23/118 [====>.........................] - ETA: 10:03 - loss: 0.0538 - accuracy: 0.9832

你可能感兴趣的:(tensorflow:自定义卷积层实现MNIST识别)