它是多重继承吗?如果不是为什么?(Is it a Multiple Inheritance?If not why?)
public interface A { class Aclass { int constants = 100; public void display() { System.out.println("Inside A"); } } public void display(); } public interface B { class Bclass { int constants = 130; public void display() { System.out.println("Inside B"); } } public void display(); } public class MultipleInheritance implements A, B { @Override public void display() { A.Aclass a = new A.Aclass(); System.out.println(a.constants); B.Bclass b = new B.Bclass(); System.out.println(b.constants); } public static void main(String args[]) { new MultipleInheritance().display(); } }
虽然它是通过接口而不是通过上下文中的具体类而不是继承任何东西,但仍然不是代码重用,即使保持内部类很难但仍然充当多重继承请用内存表示清除如果可能的话。
public interface A { class Aclass { int constants = 100; public void display() { System.out.println("Inside A"); } } public void display(); } public interface B { class Bclass { int constants = 130; public void display() { System.out.println("Inside B"); } } public void display(); } public class MultipleInheritance implements A, B { @Override public void display() { A.Aclass a = new A.Aclass(); System.out.println(a.constants); B.Bclass b = new B.Bclass(); System.out.println(b.constants); } public static void main(String args[]) { new MultipleInheritance().display(); } }
though it's through interface and not through a concrete class in context to which you are not inheriting anything but still is it not a code reuse even though maintaining a inner classes will be difficult but still it acts as a multiple-inheritance please clearify with memory representation if possible.
原文:https://stackoverflow.com/questions/18631367
最满意答案
为什么scikit-learn功能不能完成这项工作? 您将所有样本(图像)转发到列车/测试集中,将单热编码转换为标签编码(请参阅链接 ),并将其作为
y_pred
传递到y_pred
。 你以y_true
(单热标签)以类似的方式继续。示例代码:
import sklearn.metrics as metrics y_pred_ohe = KerasClassifier.predict(X) # shape=(n_samples, 12) y_pred_labels = np.argmax(y_pred_ohe, axis=1) # only necessary if output has one-hot-encoding, shape=(n_samples) confusion_matrix = metrics.confusion_matrix(y_true=y_true_labels, y_pred=y_pred_labels) # shape=(12, 12)
Here's how to get the confusion matrix(or maybe statistics using scikit-learn) for all classes:
1.Predict classes
test_generator = ImageDataGenerator() test_data_generator = test_generator.flow_from_directory( test_data_path, # Put your path here target_size=(img_width, img_height), batch_size=32, shuffle=False) test_steps_per_epoch = numpy.math.ceil(test_data_generator.samples / test_data_generator.batch_size) predictions = model.predict_generator(test_data_generator, steps=test_steps_per_epoch) # Get most likely class predicted_classes = numpy.argmax(predictions, axis=1)
2.Get ground-truth classes and class-labels
true_classes = test_data_generator.classes class_labels = list(test_data_generator.class_indices.keys())
3. Use scikit-learn to get statistics
report = metrics.classification_report(true_classes, predicted_classes, target_names=class_labels) print(report)
You can read more here
EDIT: If the above does not work, have a look at this video Create confusion matrix for predictions from Keras model. Probably look through the comments if you have an issue. Or Make predictions with a Keras CNN Image Classifier
相关问答
更多-
你是对的:如果你使用16个尺寸为3x3滤镜,你会看到16x3x6重量。 当我使用免费的HDFView工具从这里打开VGG16网络的*.h5文件时,卷积具有以下权重: 32位浮点,64 x 3 x 3 x 3 32位浮点64 对于这一层 Convolution2D(64, 3, 3, activation='relu') 因此,我认为您使用的HDF Explorer错误(这就是我要求更多屏幕截图的原因)。 请尝试在引用的要点中打开网络文件,如果使用工具看到具有相同尺寸的权重,请检查。 You are rig ...
-
CNN与Python和Keras(CNN with Python and Keras)[2023-09-02]
在这种情况下, 卷积神经网络不是最佳选择。 顺便说一句,你可以用Conv1d轻松完成这件事: model = keras.Sequential() model.add(keras.layers.Embedding(44, 100)) model.add(keras.layers.Conv1D(50, kernel_size=1, strides=1)) model.add(keras.layers.GlobalAveragePooling1D()) # model.add(keras.layers.Dens ... -
这似乎是一个非常简单的问题。 数据内部的结构非常少,所以我认为你可以尝试通过去除最后两个卷积和最大汇集层来减少神经网络的深度。 而是增加完全连接层中的节点数量,如下所示: model = Sequential() model.add(Conv2D(32, 3, activation='relu', input_shape=[100, 100, 1])) model.add(MaxPool2D()) model.add(BatchNormalization()) model.add(Conv2D(64, 3 ...
-
错误是因为你的模型定义,而不是ImageDataGenerator (我没有看到你已经发布的代码中使用)。 我假设len(classes) = 2因为你得到的错误信息。 由于模型的最后一层期望trainingLabels对每个数据点都有一个大小为2的矢量,但您的trainingLabels标签是一维数组,因此您会收到错误。 为了解决这个问题,你可以改变你的最后一层只有1个单元,因为它是二进制分类: model.add(Dense(1)) 或者您可以使用一种热门编码将您的训练和验证标签更改为矢量: from ...
-
不,它们可以是不同的名称,它取决于您如何加载数据。 在你的情况下,你可以使用flow_from_directory来生成训练数据,实际上目录将是关联的类,这是ImageDataGenerator的一部分。 No they can be of different names, it really depends on how you load your data. In your case, you can use flow_from_directory to generate the training da ...
-
我想这是你想要实现的架构的一部分: | RELU | -- > | RELU | --> | RELU | --> | RELU | | | | | | | | | ----------------> | RELU | <----------------- 假设你有一 ...
-
您可以使用summary函数找出Keras模型中(非)可训练参数的数量: from keras import models, layers model = models.Sequential() model.add(layers.Conv2D(10, (4,4), input_shape=(32, 32, 3))) model.summary() 这是输出: Layer (type) Output Shape Param # ====== ...
-
为什么scikit-learn功能不能完成这项工作? 您将所有样本(图像)转发到列车/测试集中,将单热编码转换为标签编码(请参阅链接 ),并将其作为y_pred传递到y_pred 。 你以y_true (单热标签)以类似的方式继续。 示例代码: import sklearn.metrics as metrics y_pred_ohe = KerasClassifier.predict(X) # shape=(n_samples, 12) y_pred_labels = np.argmax(y_pred_ ...
-
问题是由输出类的数量(三)与您选择的最终层激活(Sigmoid)和损失函数(二进制交叉熵)之间的不匹配引起的。 sigmoid函数将实际值“压缩”为[0,1]之间的值,但它仅针对二进制(两类)问题而设计。 对于多个类,您需要使用softmax函数之类的东西。 Softmax是sigmoid的一般化版本(当你有两个类时,这两个应该是等价的)。 损失值也需要更新为可以处理多个类的损失值 - 在这种情况下,分类交叉熵将起作用。 在代码方面,如果您将模型定义和编译代码修改为下面的版本,它应该可以工作。 model ...
-
CNN的一维数据(One dimensional data with CNN)[2022-09-19]
如果您的数据在空间上是相关的(您说它不是),那么您可以将其提供给形状为1xNx1或Nx1x1(行xcs x通道)的convnet(或者,特别是conv2d层)。 如果这根本不是空间数据 - 你只有N个非空间相关的特征,那么形状应该是1x1xN。 为了完整起见,我应该指出,如果你的数据确实是非空间的,那么使用卷积层/网络真的没有意义。 您可以将其塑造为1x1xN,然后使用1x1卷积,但由于1x1卷积与完全连接(也称为密集又称线性)层完全相同,因此您可能只需使用它。 If your data were spat ...