model.compile的功能是什么?

时间:2020-01-31 21:36:41

标签: python-3.x machine-learning

因此,我正在使用每个字母的数据集制作本地语言翻译器。我对机器学习的知识很少,只做了2类图像分类器。原来这些是我的代码,效果很好。我得到分类报告和混淆矩阵。它显示了我所有的参数和不可训练的参数,这里是代码

test

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

char

我不知道它的功能是什么,但它的作用是什么,但是我怀疑它应该是分类交叉熵,因为我正在运行多个图像分类器,但是当将其更改为String时,我什至会遇到很多错误尝试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()

有人知道我是否可以继续使用这些代码,还是应该更改它,因为即时消息的准确性真的很低?

1 个答案:

答案 0 :(得分:0)

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

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

相关问题