这里所指的均为tf.keras
acc_meter = metrics.Accuracy()
loss_meter = metrics.Mean()
loss_meter.update_state(loss)
acc_meter.udate_state(y, pred)
print(step, 'loss:', loss_meter.result().numpy())
print(step, 'Evaluate Acc:', total_correct/total, acc_meter.result().numpy())
if step % 100 == 0:
print(step, 'loss:', loss_meter.result().numpy())
loss_meter.reset_states()
if step % 500 == 0:
acc_meter.reset_states()
network.compile(optimizer=optimizers.Adam(lr=0.01),
loss=tf.loss.CategoricalCrossentropy(from_logits=True),
merics=['accuracy']
)
network.fit(db, epochs=10)
network.fit(db, epochs=10, validation_data=ds_val, validation_steps=2)
network.evaluate(ds_val)
pred = network.predict(x)
pred = tf.argmax(pred, axis=1)
需要继承自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()
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
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
# 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)
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)
###可移植性比较强
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])))