Tensorflow Keras-AttributeError:图层要素没有入站节点 [英] Tensorflow Keras - AttributeError: Layer features has no inbound nodes
问题描述
Tensorflow版本:1.11.0
Tensorflow version : 1.11.0
我正在尝试将TensorBoard与Tensorflow keras模型一起用于投影仪的可视化. 我收到AttributeError:图层要素没有入站节点. 我不确定为什么在下面的简单代码中会出现此错误.我确实在Google上搜索了该错误,但是找不到正确的解决方案来修复它.
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])
推荐答案
在Keras中定义网络时,添加的第一层需要添加input_shape.
When defining a network in Keras, the first layer added needs to have input_shape added.
在此处查看文档: https://keras.io/getting-started/sequential-model-guide/#specifying-the-input-shape
因此,对于MNIST,您应该输入input_shape =(28,28,1)
So for MNIST, you should have something like input_shape=(28,28,1)
这里有一个很好的例子: https://www .kaggle.com/adityaecdrid/mnist-with-keras-for-beginners-99457
There's a nice example here: https://www.kaggle.com/adityaecdrid/mnist-with-keras-for-beginners-99457
这篇关于Tensorflow Keras-AttributeError:图层要素没有入站节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!