How to fix Keras ValueError: Shapes (None, 3, 2) and (None, 2) are incompatible?
How to fix Keras ValueError: Shapes (None, 3, 2) and (None, 2) are incompatible?
下面的代码给我错误 ValueError: Shapes (None, 3, 2) and (None, 2) are incompatible
。我想做的是构建一个多任务网络。我该如何解决?我正在使用 Tensorflow 2.3.0.
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout
from tensorflow.keras import Model
base_model = tf.keras.applications.EfficientNetB7(input_shape=(32,32, 3), weights='imagenet',
include_top=False) # or weights='noisy-student'
for layer in base_model.layers[:]:
layer.trainable = False
x = GlobalAveragePooling2D()(base_model.output)
dropout_rate = 0.3
x = Dense(256, activation='relu')(x)
x = Dropout(dropout_rate)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(dropout_rate)(x)
all_target = []
loss_list = []
test_metrics = {}
for name, node in [("task1", 2), ("task2", 2), ("task3", 2)]:
y1 = Dense(128, activation='relu')(x)
y1 = Dropout(dropout_rate)(y1)
y1 = Dense(64, activation='relu')(y1)
y1 = Dropout(dropout_rate)(y1)
# y1 = Dense(64, activation='relu')(y1)
# y1 = Dropout(dropout_rate)(y1)
y1 = Dense(node, activation='softmax', name=name)(y1)
all_target.append(y1)
loss_list.append('categorical_crossentropy')
test_metrics[name] = "accuracy"
# model = Model(inputs=model_input, outputs=[y1, y2, y3])
model = Model(inputs=base_model.input, outputs=all_target)
model.compile(loss=loss_list, optimizer='adam', metrics=test_metrics)
res=np.random.randint(2, size=3072).reshape(32, 32, 3)
res=np.expand_dims(res, 0)
lab=np.array([[[0,1], [0,1], [0,1]]])
history = model.fit(res, y=lab, epochs=1, verbose=1)
如您所想,错误是由目标的形状引起的。 Keras 期望以下内容:
A list of 3 NumPy arrays (for your three tasks), of shape (n_samples, n_categories)
训练将成功 运行 这一行:
lab = [np.array([[0, 1]]), np.array([[0, 1]]), np.array([[0, 1]])]
我们有不同的版本,但是当 运行使用你的代码时,我遇到了一个信息更丰富的错误:
ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), for inputs ['task1', 'task2', 'task3'] but instead got the following list of 1 arrays: [array([[[0, 1],
[0, 1],
[0, 1]]])]...
下面的代码给我错误 ValueError: Shapes (None, 3, 2) and (None, 2) are incompatible
。我想做的是构建一个多任务网络。我该如何解决?我正在使用 Tensorflow 2.3.0.
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout
from tensorflow.keras import Model
base_model = tf.keras.applications.EfficientNetB7(input_shape=(32,32, 3), weights='imagenet',
include_top=False) # or weights='noisy-student'
for layer in base_model.layers[:]:
layer.trainable = False
x = GlobalAveragePooling2D()(base_model.output)
dropout_rate = 0.3
x = Dense(256, activation='relu')(x)
x = Dropout(dropout_rate)(x)
x = Dense(256, activation='relu')(x)
x = Dropout(dropout_rate)(x)
all_target = []
loss_list = []
test_metrics = {}
for name, node in [("task1", 2), ("task2", 2), ("task3", 2)]:
y1 = Dense(128, activation='relu')(x)
y1 = Dropout(dropout_rate)(y1)
y1 = Dense(64, activation='relu')(y1)
y1 = Dropout(dropout_rate)(y1)
# y1 = Dense(64, activation='relu')(y1)
# y1 = Dropout(dropout_rate)(y1)
y1 = Dense(node, activation='softmax', name=name)(y1)
all_target.append(y1)
loss_list.append('categorical_crossentropy')
test_metrics[name] = "accuracy"
# model = Model(inputs=model_input, outputs=[y1, y2, y3])
model = Model(inputs=base_model.input, outputs=all_target)
model.compile(loss=loss_list, optimizer='adam', metrics=test_metrics)
res=np.random.randint(2, size=3072).reshape(32, 32, 3)
res=np.expand_dims(res, 0)
lab=np.array([[[0,1], [0,1], [0,1]]])
history = model.fit(res, y=lab, epochs=1, verbose=1)
如您所想,错误是由目标的形状引起的。 Keras 期望以下内容:
A list of 3 NumPy arrays (for your three tasks), of shape (n_samples, n_categories)
训练将成功 运行 这一行:
lab = [np.array([[0, 1]]), np.array([[0, 1]]), np.array([[0, 1]])]
我们有不同的版本,但是当 运行使用你的代码时,我遇到了一个信息更丰富的错误:
ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), for inputs ['task1', 'task2', 'task3'] but instead got the following list of 1 arrays: [array([[[0, 1], [0, 1], [0, 1]]])]...