Tensorflow Keras - AttributeError: Layer features has no inbound nodes

元气小坏坏 提交于 2020-05-29 06:01:25

问题


Tensorflow version : 1.11.0

I am trying to use TensorBoard with Tensorflow keras model for projector visualisation. I am getting AttributeError: Layer features has no inbound nodes. I am not sure why I get this error in below simple code. I indeed google the error but I could not find right solution to fix it.

from os import makedirs
from os.path import exists, join
import tensorflow as tf
mnist = tf.keras.datasets.mnist

import numpy as np


(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(512, activation=tf.nn.relu),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(10, activation=tf.nn.relu, name='features'),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

log_dir = "./logs"
with open(join(log_dir, 'metadata.tsv'), 'w') as f:
    np.savetxt(f, y_test)

from tensorflow.keras.callbacks import TensorBoard

tf_board_callback = TensorBoard(
                            log_dir=log_dir,
                            batch_size=32,
                            embeddings_freq=1,
                            embeddings_layer_names=['features'],
                            embeddings_metadata='metadata.tsv',
                            embeddings_data=x_test
                        )

model.fit(x_train, y_train, epochs=5, callbacks=[tf_board_callback])

回答1:


When defining a network in Keras, the first layer added needs to have input_shape added.

See the docs here: https://keras.io/getting-started/sequential-model-guide/#specifying-the-input-shape

So for MNIST, you should have something like input_shape=(28,28,1)

There's a nice example here: https://www.kaggle.com/adityaecdrid/mnist-with-keras-for-beginners-99457




回答2:


I guess you should specify input shape for the first layer of the sequential model



来源:https://stackoverflow.com/questions/52754453/tensorflow-keras-attributeerror-layer-features-has-no-inbound-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!