Если вы хотите настроить свою собственную модель с помощью предварительно обученной модели с 5 классами, вы, вероятно, захотите добавить еще один слой , чтобы проецировать 5 классов на 21 класс.
Ошибка, которую вы видите, связана с тем, что вы, вероятно, не определили новый набор «output_weights» и «output_bias», но повторно использовали их для своих новых меток с 21 классом. Ниже я "префикс" промежуточных тензоров для ваших новых ярлыков с "final _".
Код должен выглядеть примерно так:
# These are the logits for the 5 classes. Keep them as is.
logits = tf.matmul(output_layer, output_weights, transpose_b=True)
logits = tf.nn.bias_add(logits, output_bias)
# You want to create one more layer
final_output_weights = tf.get_variable(
"final_output_weights", [21, 5],
initializer=tf.truncated_normal_initializer(stddev=0.02))
final_output_bias = tf.get_variable(
"final_output_bias", [21], initializer=tf.zeros_initializer())
final_logits = tf.matmul(logits, final_output_weights, transpose_b=True)
final_logits = tf.nn.bias_add(final_logits, final_output_bias)
# Below is for evaluating the classification.
final_probabilities = tf.nn.softmax(final_logits, axis=-1)
final_log_probs = tf.nn.log_softmax(final_logits, axis=-1)
# Note labels below should be the 21 class ids.
final_one_hot_labels = tf.one_hot(labels, depth=21, dtype=tf.float32)
final_per_example_loss = -tf.reduce_sum(final_one_hot_labels * final_log_probs, axis=-1)
final_loss = tf.reduce_mean(final_per_example_loss)