经过Phillippe Remy's PhasedLSTMCell
example的努力,我想知道是否可以使用更高级别的Tensorflow Keras API在各层中使用PhasedLSTM
单元。
对于tf.keras.layers.RNN
docs,RNN
层应接受
“一个RNN单元实例或一个RNN单元实例列表[,]”,所以我希望这样的方法可以起作用:
plstm = tf.keras.layers.RNN(tf.contrib.rnn.PhasedLSTMCell(32))([time, reshaped])
但是它抛出:
ValueError: An `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`=[InputSpec(shape=(None, 784, 1), ndim=3)]; however `cell.state_size` is [32, 32]
(下面是完整的MWE。)
是否可以在图层中使用PhasedLSTMCell
?否则我会搞砸了吗?
import numpy as np
import tensorflow as tf
def build_evenly_space_t(mnist_img_size, batch_size):
return np.reshape(np.tile(np.array(range(mnist_img_size)), (batch_size, 1)), (batch_size, mnist_img_size, 1))
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# This works
imgs = tf.keras.layers.Input(shape=(28, 28))
reshaped = tf.keras.layers.Reshape((28 * 28, 1), input_shape=(28*28,))(imgs)
lstm = tf.keras.layers.RNN(tf.nn.rnn_cell.LSTMCell(32))(reshaped)
out = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(lstm)
this_model_will = tf.keras.models.Model(inputs=imgs, outputs=out)
this_model_will.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# this does not
time = tf.keras.layers.Input(shape=(1,))
imgs = tf.keras.layers.Input(shape=(28, 28))
reshaped = tf.keras.layers.Reshape((28 * 28, 1), input_shape=(28*28,))(imgs)
plstm = tf.keras.layers.RNN(tf.contrib.rnn.PhasedLSTMCell(32))([time, reshaped])
out = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(plstm)
this_model_will_not = tf.keras.models.Model(inputs=[time, imgs], outputs=out)
this_model_will_not.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])