gpt4 book ai didi

python-3.x - model.compile 的作用是什么?

转载 作者:行者123 更新时间:2023-11-30 09:13:57 33 4
gpt4 key购买 nike

所以我正在使用每个字母的数据集制作一个土著语言翻译器。我对机器学习的了解很少,只制作了 2 类图像分类器。最初这些是我的代码,它工作正常。我得到分类报告和混淆矩阵。它显示了我所有的参数和不可训练的参数,这里是代码

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from tensorflow.keras.optimizers import Adam
from PIL import ImageFile, Image
print(Image.__file__)
import numpy
import matplotlib.pyplot as plt

# dimensions of our images.
img_width, img_height = 150, 150

train_data_dir = r'C:\Users\Acer\imagerec\BAYBAYIN\TRAIN'
validation_data_dir = r'C:\Users\Acer\imagerec\BAYBAYIN\VAL'
nb_train_samples = 51600
nb_validation_samples = 12900
epochs = 1
batch_size = 100

if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)

from keras.applications.xception import Xception
from keras.models import Model
from keras.layers import Dense

vgg = Xception(include_top=False, weights='imagenet', input_shape=(), pooling='avg')
x = vgg.output
x = Dense(1, activation='softmax')(x)
model = Model(vgg.input, x)
model.summary()

model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=.0001),
metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)

print("PRINTING OUT CLASSIFICATION REPORT AND CONFUSION MATRIX")

from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

test_steps_per_epoch = numpy.math.ceil(validation_generator.samples / validation_generator.batch_size)

predictions = model.predict_generator(validation_generator, steps=test_steps_per_epoch)
# Get most likely class
predicted_classes = numpy.argmax(predictions, axis=1)
true_classes = validation_generator.classes
class_labels = list(validation_generator.class_indices.keys())
report = classification_report(true_classes, predicted_classes, target_names=class_labels)
print(report)

cm=confusion_matrix(true_classes,predicted_classes)
print(cm)

plt.imshow(cm)

import matplotlib.pyplot as plt
import numpy as np


plt.imshow(np.random.random((48,48)), interpolation='nearest')
plt.xticks(np.arange(0,48), ['A', 'BA', 'KA', 'GA', 'HA', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
'20', '21', '22', '23', '24', '25', '26', '28', '29', '30', '31', '32',
'33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44'])
plt.yticks(np.arange(0,48),['A', 'BA', 'KA', 'GA', 'HA', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
'20', '21', '22', '23', '24', '25', '26', '28', '29', '30', '31', '32',
'33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44'])

plt.show()

我只是对这些特定的代码行有疑问

model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=.0001),
metrics=['accuracy'])

我不知道它的功能是什么或它的用途是什么,但我怀疑它应该是一个分类交叉熵,因为我正在运行多个图像分类器,但是当将其更改为categorical_ crossentropy时,我即使尝试 sparse_categorical_crossentropy

也会出现很多错误

有谁知道我是否可以继续使用这些代码,或者我应该更改它,因为我也遇到了准确性很低的问题

最佳答案

Model.compile 仅用于配置模型的损失函数、优化、损失指标、损失权重等)

你绝对应该使用分类交叉熵;您能否粘贴错误 - 您的模型可能由于各种原因而表现不佳。

关于python-3.x - model.compile 的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60011557/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com