问题
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