TensorFlow2.x 学习笔记(七)Keras高层接口

Keras高层接口

这里所指的均为tf.keras

Keras高层API

metrics

acc_meter = metrics.Accuracy()
loss_meter = metrics.Mean()

update_state

loss_meter.update_state(loss)
acc_meter.udate_state(y, pred)

result().numpy()

print(step, 'loss:', loss_meter.result().numpy())
print(step, 'Evaluate Acc:', total_correct/total, acc_meter.result().numpy())

reset_states

if step % 100 == 0:
    print(step, 'loss:', loss_meter.result().numpy())
    loss_meter.reset_states()

if step % 500 == 0:
    acc_meter.reset_states()

complie & fit

Compile

network.compile(optimizer=optimizers.Adam(lr=0.01),
        loss=tf.loss.CategoricalCrossentropy(from_logits=True),
        merics=['accuracy']
    )

Fit

network.fit(db, epochs=10)

Evaluate

network.fit(db, epochs=10, validation_data=ds_val, validation_steps=2)
network.evaluate(ds_val) 

Predict

pred = network.predict(x) 
pred = tf.argmax(pred, axis=1)

自定义网络

keras.Sequential

需要继承自Layer

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.trainable_variables
model.call()

keras.layers.Layer

Inherit from keras.layers.Layer

__init__
call
# example
class MyDense(layers.Layer):
    def __init__(self, inp_dim, outp_dim):
        super(MyDense, self).__init__()

        self.kernel = self.add_variable('w', [inp_dim, outp_dim])
        self.bias = self.add_variable('b', [outp_dim])

    def call(self, inputs, training=None):
        out = inputs @ self.kernel + self.bias
        return out 

keras.Model

Inherit from keras.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

模型的保存与加载

save/load weights

# training...
############
network.save_weights('./checkpoints/weights.ckpt')
del network

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.compile(optimizer=optimizers.Adam(lr=0.01),
		loss=tf.losses.CategoricalCrossentropy(from_logits=True),
		metrics=['accuracy']
	)
network.load_weights('./checkpoints/weights.ckpt')
network.evaluate(ds_val)

save/load entire model

network.save('model.h5')
del network

network = tf.keras.models.load_model('model.h5', compile=False)
network.compile(optimizer=optimizers.Adam(lr=0.01),
        loss=tf.losses.CategoricalCrossentropy(from_logits=True),
        metrics=['accuracy']
    )
network.evaluate(ds_val)

saved_model

###可移植性比较强
tf.saved_model.save(m, '/tmp/saved_model')
imported = tf.saved_model.load(path)
f = imported.dignatures["servinf_default"]
print(f(x=tf.ones([1, 28, 28, 3])))

你可能感兴趣的:(tensorflow)