I have three learners (three different pre-trained models) participating in an ensemble.
For my train dataset, I get a list of NumPy arrays that state the confidence of each model
# for 3 classes A,B,and C
# prediction array of a given model for 4 different train set images:
[[0.2, 0.2, 0.6],
[0.1, 0.8, 0.1],
[0.4, 0.4, 0.2],
[0.3, 0.3, 0.4]]
I get 3 prediction arrays M1, M2, M3 from 3 different models from which a final prediction array will be generated using np.argmax on the weights
M = w1 * M1 + w2 * M2 + w3 * M3 (w3= 1-w1-w2)
What I planned on, to find these weights was to use another neural network, and compare this with the label array for optimizing loss function (using one hot encoding on it)
[[0.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0]]
The code I presented with uses the 3 prediction lists (with NumPy array confidence per class) in a pandas dataframe. I check it against the list of correct labels (one hot encoded) using an ann
X= pd.DataFrame()
X.insert(0, '1', label1, False)
X.insert(1, '2', label2, False)
X.insert(2, '3', label3, False)
X= X.iloc[1:, :]
The problem is the input shape that I am trying to fit gives the error
Failed to convert a NumPy array to a Tensor (Unsupported object type tensorflow.python.framework.ops.EagerTensor)
ensemble model I created:
model_ensemble = tf.keras.models.Sequential()
model_ensemble.add(tf.keras.layers.Dense(units=10, activation='relu'))
model_ensemble.add(tf.keras.layers.Dense(units=10, activation='relu'))
model_ensemble.add(tf.keras.layers.Dense(units=3, activation='softmax'))
model_ensemble.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model_ensemble.fit(X, a, batch_size = 32, epochs = 25)
for creating the correct labels to compare to:
a= pd.DataFrame(x_train['label'] ).astype('int').astype('category')
print(a)
a= tf.keras.utils.to_categorical(a)
I am also attaching the way I generate the prediction labels, which is probably why this stuff isn't working
label1= [np.asarray([])]
label2= [np.asarray([])]
label3= [np.asarray([])]
for i in x_train['name']:
Path= path.join('/content/data/train', I)
img = tf.io.read_file(Path)
tensor = tf.io.decode_image(img, channels=3, dtype=tf.dtypes.float32)
tensor = tf.image.resize(tensor, [224, 224])
input_tensor = tf.expand_dims(tensor, axis=0)
result1=model1.predict(input_tensor)
result2=model2.predict(input_tensor)
result3=model3.predict(input_tensor)
label1.append(tf.convert_to_tensor(result1))
label2.append(tf.convert_to_tensor(result2))
label3.append(tf.convert_to_tensor(result3))