我正在尝试创建一个在其中使用某些Keras
函数的Tensorflow
模型。
该模型基于Keras MNIST CNN
示例,它由一个简单的卷积层堆栈和一个Gaussian
的估计组成。为了评估Gaussian
函数,我使用Dense(2)
估计其均值和标准差。然后,使用tf.range
和tf.exp
将这两个参数组合在一起,形成一个Gaussian
函数。然后将此Gaussian
张量与网络相乘,以产生最终输出。
如果我运行的模型没有Gaussian
乘法,则一切正常。当我引入Gaussian
位时,出现错误:
RuntimeError: Graph disconnected: cannot obtain value for tensor
Tensor("Exp_2:0", shape=(?, 9216), dtype=float32) at layer "tf". The
following previous layers were accessed without issue: ['input_2',
'conv2d_3', 'conv2d_4', 'max_pooling2d_2', 'dropout_3', 'flatten_2']
这是一个完全可复制的脚本:
'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Model, Input
from keras.layers import Dense, Dropout, Flatten, Multiply
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
inputs = Input(shape=input_shape)
net = Conv2D(32, kernel_size=(3, 3),
activation='relu')(inputs)
net = Conv2D(64, (3, 3), activation='relu')(net)
net = MaxPooling2D(pool_size=(2, 2))(net)
net = Dropout(0.25)(net)
net = Flatten()(net)
gauss_par = Dense(2, activation='relu')(net)
# Define a vector from 1 to n_frames
x1d = K.tf.range(0, 9216, dtype='float32')
# Transform the vector into a tensor of shape [batch_size, n_frames]
x = K.tf.tile(K.tf.reshape(x1d, [1, -1]), [K.tf.shape(inputs)[0], 1])
# Gaussian function
gaussian = K.tf.exp(-K.tf.pow(x - gauss_par[:, 0], 2) / 2 / K.tf.pow(gauss_par[:, 1], 2))
# Transform the tensorflow tensor into a keras tensor
gaussian_layer = Input(tensor=gaussian, name='tf')
net = Multiply()([net, gaussian_layer])
net = Dense(128, activation='relu')(net)
net = Dropout(0.5)(net)
outputs = Dense(num_classes, activation='softmax')(net)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.summary()
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
您知道什么可能导致此错误吗?