model.compile 函数的作用是什么?

我正在使用按字母的数据集制作一个土著语言翻译器。我对机器学习的了解很少,之前只做过一个两类图像分类器。最初我的代码运行得很好。我得到了分类报告和混淆矩阵。它显示了我的所有参数和不可训练的参数,以下是我的代码

from keras.preprocessing.image import ImageDataGeneratorfrom keras.models import Sequentialfrom keras.layers import Conv2D, MaxPooling2Dfrom keras.layers import Activation, Dropout, Flatten, Densefrom keras import backend as Kfrom tensorflow.keras.optimizers import Adamfrom PIL import ImageFile, Imageprint(Image.__file__)import numpyimport matplotlib.pyplot as plt# dimensions of our images.img_width, img_height = 150, 150train_data_dir = r'C:\Users\Acer\imagerec\BAYBAYIN\TRAIN'validation_data_dir = r'C:\Users\Acer\imagerec\BAYBAYIN\VAL'nb_train_samples = 51600nb_validation_samples = 12900epochs = 1batch_size = 100if 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 Xceptionfrom keras.models import Modelfrom keras.layers import Densevgg = Xception(include_top=False, weights='imagenet', input_shape=(), pooling='avg')x = vgg.outputx = 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 trainingtrain_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 rescalingtest_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_reportfrom sklearn.metrics import confusion_matrixtest_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 classpredicted_classes = numpy.argmax(predictions, axis=1)true_classes = validation_generator.classesclass_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 pltimport numpy as npplt.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 只是用来配置模型的损失函数、优化方法、损失度量、损失权重等。

你绝对应该使用分类交叉熵;你能贴出错误吗?你的模型表现不佳的原因可能有很多。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注