溫馨提示×

TensorFlow中怎么評估模型性能

小億
96
2024-05-10 15:09:58

在TensorFlow中,可以使用tf.keras.Model.evaluate方法來評估模型的性能。這個方法會返回模型在測試數(shù)據(jù)上的損失值和指定的評估指標(biāo)的值。

以下是一個示例代碼,演示如何使用evaluate方法評估模型性能:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# 創(chuàng)建一個簡單的全連接網(wǎng)絡(luò)模型
model = Sequential([
    Dense(64, activation='relu', input_shape=(10,)),
    Dense(64, activation='relu'),
    Dense(1, activation='sigmoid')
])

# 編譯模型
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# 加載測試數(shù)據(jù)
test_data = ...
test_labels = ...

# 評估模型性能
loss, accuracy = model.evaluate(test_data, test_labels)

print('Test loss:', loss)
print('Test accuracy:', accuracy)

在上面的示例中,首先創(chuàng)建了一個簡單的全連接網(wǎng)絡(luò)模型,然后編譯模型并加載測試數(shù)據(jù)。最后調(diào)用evaluate方法評估模型在測試數(shù)據(jù)上的性能,并打印出損失值和準(zhǔn)確率。

0