简体   繁体   中英

AttributeError: Layer mnist_model_35 has no inbound nodes. Tensorflow keras subclassing API

I'm doing keras subclassing with the mnist dataset. I was able to make it with Sequantial and Functional api's. But now when i call model.fit() on my subclass i get this error:

AttributeError: Layer mnist_model_35 has no inbound nodes.

This is my code:

  1. MNIstModel
class MNISTModel(keras.Model):
  def __init__(self):
    super().__init__()
    self.flatten_layer = keras.layers.Flatten()
    self.dense_1 = keras.layers.Dense(64, activation='relu')
    self.dense_2 = keras.layers.Dense(128, activation='relu')
    self.relu = keras.activations.relu
    self.ouput = keras.layers.Dense(10, activation='softmax')
    self.softmax = keras.activations.softmax

  def call(self, x):
    x = self.flatten_layer(x)
    x = self.dense_1(x)
    x = self.dense_2(x)
    x = self.output(x)
    return x
  def model(self):
    x = keras.layers.Input(shape=(28*28,))
    return keras.Model(inputs=[x], outputs=self.call(x))

  1. Trainning my model :
sub_model = MNISTModel()
sub_model_1 = Model(sub_model)
sub_model.compile(
    loss = keras.losses.SparseCategoricalCrossentropy(from_logits=False),
    optimizer = keras.optimizers.Adam(learning_rate=0.001),
    metrics = keras.metrics.SparseCategoricalAccuracy()           
)
sub_model.fit(X_train_tensors, y_train_tensors, epochs=2, verbose=1, batch_size=32,
          validation_data=(X_test_tensors, y_test_tensors),
          validation_batch_size=16)

sub_model.model().summary()
  1. How i loaded and preprocess my mnist dataset.
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()

def normalize(image):
  image = tf.convert_to_tensor(image.astype('float32'))/255
  return image

X_train_tensors =tf.convert_to_tensor(list(map(normalize, X_train)))
X_test_tensors = tf.convert_to_tensor(list(map(normalize, X_test)))

y_test_tensors = tf.convert_to_tensor(y_test)
y_train_tensors = tf.convert_to_tensor(y_train)

You are lacking the input_shape on your first layer:

class MNISTModel(keras.Model):
    def __init__(self):
        super().__init__()
        self.flatten_layer = keras.layers.Flatten(input_shape=(28, 28))
        self.dense_1 = keras.layers.Dense(64, activation='relu')
        self.dense_2 = keras.layers.Dense(128, activation='relu')
        self.op = keras.layers.Dense(10, activation='softmax')

    def call(self, x):
        y = self.flatten_layer(x)
        y = self.dense_1(y)
        y = self.dense_2(y)
        return self.op(y)


sub_model = MNISTModel()
sub_model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=False),
    optimizer=keras.optimizers.Adam(learning_rate=0.001),
    metrics=[keras.metrics.SparseCategoricalAccuracy()]
)
sub_model.fit(X_train_tensors, y_train_tensors, epochs=2, verbose=1, batch_size=32,
              validation_data=(X_test_tensors, y_test_tensors),
              validation_batch_size=16)

sub_model.summary()

There is some syntax error issue in your code. Here is the correct one.

class MNISTModel(keras.Model):
  def __init__(self):
    super().__init__()
    self.flatten_layer = keras.layers.Flatten()
    self.dense_1 = keras.layers.Dense(64, activation='relu')
    self.dense_2 = keras.layers.Dense(128, activation='relu')
    self.out = keras.layers.Dense(10, activation='softmax')

  def call(self, x):
    x = self.flatten_layer(x)
    x = self.dense_1(x)
    x = self.dense_2(x)
    x = self.out(x)
    return x
  def model(self):
    x = keras.layers.Input(shape=(28, 28))
    return keras.Model(inputs=[x], outputs=self.call(x))
sub_model = MNISTModel()
sub_model.model().summary()

Running

(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()

sub_model.compile(
    loss = keras.losses.SparseCategoricalCrossentropy(from_logits=False),
    optimizer = keras.optimizers.Adam(learning_rate=0.001),
    metrics = keras.metrics.SparseCategoricalAccuracy()           
)
sub_model.fit(X_train, y_train, epochs=2, verbose=1, batch_size=32,
          validation_data=(X_test, y_test))
Epoch 1/2
5s 2ms/step - loss: 1.2542 - sparse_categorical_accuracy: 0.8238 - 
 val_loss: 0.4759 - val_sparse_categorical_accuracy: 0.8937

Epoch 2/2
4s 2ms/step - loss: 0.4178 - sparse_categorical_accuracy: 0.9019 - 
 val_loss: 0.4046 - val_sparse_categorical_accuracy: 0.9082
<tensorflow.python.keras.callbacks.History at 0x7f6c6ed99d50>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM