目录
* Keras != tf.keras
<https://www.cnblogs.com/nickchen121/p/10921824.html#keras-tf.keras>
* Outline1 <https://www.cnblogs.com/nickchen121/p/10921824.html#outline1>
* Metrics <https://www.cnblogs.com/nickchen121/p/10921824.html#metrics>
* Step1.Build a meter
<https://www.cnblogs.com/nickchen121/p/10921824.html#step1.build-a-meter>
* Step2.Update data
<https://www.cnblogs.com/nickchen121/p/10921824.html#step2.update-data>
* Step3.Get Average data
<https://www.cnblogs.com/nickchen121/p/10921824.html#step3.get-average-data>
* Clear buffer
<https://www.cnblogs.com/nickchen121/p/10921824.html#clear-buffer>
* Outline2 <https://www.cnblogs.com/nickchen121/p/10921824.html#outline2>
* Compile + Fit
<https://www.cnblogs.com/nickchen121/p/10921824.html#compile-fit>
* Individual loss and optimize1
<https://www.cnblogs.com/nickchen121/p/10921824.html#individual-loss-and-optimize1>
* Now1 <https://www.cnblogs.com/nickchen121/p/10921824.html#now1>
* Individual epoch and step2
<https://www.cnblogs.com/nickchen121/p/10921824.html#individual-epoch-and-step2>
* Now2 <https://www.cnblogs.com/nickchen121/p/10921824.html#now2>
* Standard Progressbar
<https://www.cnblogs.com/nickchen121/p/10921824.html#standard-progressbar>
* Individual evaluation3
<https://www.cnblogs.com/nickchen121/p/10921824.html#individual-evaluation3>
* Now3 <https://www.cnblogs.com/nickchen121/p/10921824.html#now3>
* Evaluation <https://www.cnblogs.com/nickchen121/p/10921824.html#evaluation>
* Test <https://www.cnblogs.com/nickchen121/p/10921824.html#test>
* Predict <https://www.cnblogs.com/nickchen121/p/10921824.html#predict>
Keras != tf.keras
*
Keras是一个框架
*
datasets
*
layers
*
losses
*
metrics
*
optimizers
Outline1
*
Metrics
*
update_state
*
result().numpy()
*
reset_states
Metrics
Step1.Build a meter
acc_meter = metrics.Accuarcy() loss_meter = metrics.Mean
Step2.Update data
loss_meter.update_state(loss) acc_meter.update_state(y,pred)
Step3.Get Average data
print(step, 'loss:', loss_meter.result().numpy()) # ... print(step,'Evaluate
Acc:', total_correct/total, acc_meter.result().numpy()
Clear buffer
if step % 100 == 0: print(step, 'loss:', loss_meter.result().numpy())
loss_meter.reset_states() # ... if step % 500 == 0: total, total_correct = 0.,
0 acc_meter.reset_states()
Outline2
*
Compile
*
Fit
*
Evaluate
*
Predict
Compile + Fit
Individual loss and optimize1
with tf.GradientTape() as tape: x = tf.reshape(x, (-1, 28*28)) out =
network(x) y_onehot = tf.one_hot(y, depth=10) loss =
tf.reduce_mean(tf.losses.categorical_crossentropy(y_onehot, out,
from_logits=True)) grads = tape.gradient(loss, network.trainable_variables)
optimizer.apply_gradients(zip(grads, network.trainable_variables))
Now1
network.compile(optimizer=optimizers.Adam(lr=0.01),
loss=tf.losses.CategoricalCrossentropy(fromlogits=True), metircs=['accuracy'])
Individual epoch and step2
for epoch in range(epochs): for step, (x, y) in enumerate(db): # ...
Now2
network.compile(optimizer=optimizers.Adam(lr=0.01),
loss=tf.losses.CategoricalCrossentropy(fromlogits=True), metircs=['accuracy'])
network.fit(db, epochs=10)
Standard Progressbar
Individual evaluation3
if step % 500 == 0: total, total_correct = 0., 0 for step, (x, y) in
enumerate(ds_val): x = tf.reshape(x, (-1, 28*28)) out = network(x) pred =
tf.argmax(out, axis=1) pred = tf.cast(pred, dtype=tf.int32) correct =
tf.equal(pred, y) total_correct += tf.reduce_sum(tf.cast(correct,
dtype=tf.int32)).numpy() total += x.shape[0] print(step, 'Evaluate Acc:',
total_correct/total)
Now3
network.compile(optimizer=optimizers.Adam(lr=0.01),
loss=tf.losses.CategoricalCrossentropy(fromlogits=True), metircs=['accuracy'])
# validation_freq=2表示2个epochs做一次验证 network.fit(db, epochs=10,
validation_data=ds_val, validation_freq=2)
Evaluation
Test
network.compile(optimizer=optimizers.Adam(lr=0.01),
loss=tf.losses.CategoricalCrossentropy(fromlogits=True), metircs=['accuracy'])
# validation_freq=2表示2个epochs做一次验证 network.fit(db, epochs=10,
validation_data=ds_val, validation_freq=2) network.evaluate(ds_val)
Predict
sample = next(iter(ds_val)) x = sample[0] y = sample[1] pred =
network.predict(x) y = tf.argmax(y, axis=1) pred = tf.argmax(pre, axis=1)
print(pred) print(y)
热门工具 换一换