使用tensorflow中的keras(自定义model)
import tensorflow as tf from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics from tensorflow import keras def preprocess(x, y): """ x is a simple image, not a batch """ x = tf.cast(x, dtype=tf.float32) / 255. x = tf.reshape(x, [28 * 28]) y = tf.cast(y, dtype=tf.int32) y = tf.one_hot(y, depth=10) return x, y batchsz = 128 (x, y), (x_val, y_val) = datasets.mnist.load_data() print('datasets:', x.shape, y.shape, x.min(), x.max()) #输出datasets: (60000, 28, 28) (60000,) 0 255 db = tf.data.Dataset.from_tensor_slices((x, y)) #它的作用是切分传入Tensor的第一个维度,生成相应的dataset。 db = db.map(preprocess).shuffle(60000).batch(batchsz) ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val)) ds_val = ds_val.map(preprocess).batch(batchsz) sample = next(iter(db)) print(sample[0].shape, sample[1].shape) #输出(128, 784) (128, 10) # 通过Sequential容器方便的封装成一个网络模型 network = Sequential([layers.Dense(256, activation='relu'), layers.Dense(128, activation='relu'), layers.Dense(64, activation='relu'), layers.Dense(32, activation='relu'), layers.Dense(10)]) network.build(input_shape=(None, 28 * 28)) network.summary() # 输出Model: "sequential" # _________________________________________________________________ # Layer (type) Output Shape Param # # ================================================================= # dense (Dense) multiple 200960 # _________________________________________________________________ # dense_1 (Dense) multiple 32896 # _________________________________________________________________ # dense_2 (Dense) multiple 8256 # _________________________________________________________________ # dense_3 (Dense) multiple 2080 # _________________________________________________________________ # dense_4 (Dense) multiple 330 # ================================================================= # Total params: 244,522 # Trainable params: 244,522 # Non-trainable params: 0 class MyDense(layers.Layer):#创建类并且继承自layers def __init__(self, inp_dim, outp_dim): super(MyDense, self).__init__() #创建张量W和b self.kernel = self.add_weight('w', [inp_dim, outp_dim]) self.bias = self.add_weight('b', [outp_dim]) #进入training=None测试模型 def call(self, inputs, training=None): out = inputs @ self.kernel + self.bias return out #自定义网络创建类,并且继承自Model基类 class MyModel(keras.Model): def __init__(self): super(MyModel, self).__init__() self.fc1 = MyDense(28 * 28, 256) self.fc2 = MyDense(256, 128) self.fc3 = MyDense(128, 64) self.fc4 = MyDense(64, 32) self.fc5 = MyDense(32, 10) def call(self, inputs, training=None): x = self.fc1(inputs) x = tf.nn.relu(x) x = self.fc2(x) x = tf.nn.relu(x) x = self.fc3(x) x = tf.nn.relu(x) x = self.fc4(x) x = tf.nn.relu(x) x = self.fc5(x) return x network = MyModel() #model.compile()方法用于在配置训练方法时,告知训练时用的优化器、损失函数和准确率评测标准 network.compile(optimizer=optimizers.Adam(lr=0.01), loss=tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'] ) #通过fit函数进行模型训练:送入训练集、测试集、训练5个epochs,每2个epochs验证一次 network.fit(db, epochs=5, validation_data=ds_val, validation_freq=2) network.evaluate(ds_val) sample = next(iter(ds_val)) x = sample[0] y = sample[1] # one-hot pred = network.predict(x) # [b, 10] # convert back to number y = tf.argmax(y, axis=1) pred = tf.argmax(pred, axis=1) print(pred) #输出tf.Tensor( # [7 2 1 0 4 1 4 9 6 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7 # 1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4 6 4 3 0 7 0 2 9 # 1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9 6 0 5 4 9 9 2 1 9 4 8 # 7 3 9 7 4 4 4 9 2 5 4 7 6 7 9 0 5], shape=(128,), dtype=int64) print(y) # tf.Tensor( # [7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7 # 1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4 6 4 3 0 7 0 2 9 # 1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9 6 0 5 4 9 9 2 1 9 4 8 # 7 3 9 7 4 4 4 9 2 5 4 7 6 7 9 0 5], shape=(128,), dtype=int64)训练效果