如何将定制的tensorflow层嵌入keras模型

时间:2018-12-05 21:58:33

标签: python tensorflow keras keras-layer

以下是我要实现的一个简单示例的代码:

错误:引发TypeError(“输入必须为序列”),TypeError:输入必须为序列

该如何解决才能使程序正常工作?任何帮助将不胜感激。

from keras.models import Sequential
from keras.layers import LSTM, Dense, Flatten
import numpy as np
from keras.engine.topology import Layer
import tensorflow as tf


class MyLayer(Layer):

    def __init__(self, **kwargs):
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        super(MyLayer, self).build(input_shape)  

    def call(self, x):
        "Some other tf function will be put at here"
        outputs, state = tf.contrib.rnn.static_rnn(tf.contrib.rnn.LSTMBlockCell(32), x, dtype=tf.float32)
        return outputs

    def compute_output_shape(self, input_shape):
        return input_shape


def get_model(timesteps, data_dim):
    model = Sequential()
    model.add(LSTM(32, return_sequences=True, input_shape=(timesteps, data_dim)))
    model.add(LSTM(32, return_sequences=True))
    model.add(MyLayer())  # this is my layer

    model.add(Flatten())
    model.add(Dense(10, activation='softmax'))
    return model


def run_demo():
    data_dim = 16
    timesteps = 8
    num_classes = 10

    model = get_model(timesteps, data_dim)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    """Generate the traning and validation data"""
    x_train = np.random.random((1000, timesteps, data_dim))
    y_train = np.random.random((1000, num_classes))
    x_val = np.random.random((100, timesteps, data_dim))
    y_val = np.random.random((100, num_classes))

    model.fit(x_train, y_train, batch_size=64, epochs=5, validation_data=(x_val, y_val))


if __name__ == "__main__":
    run_demo()

1 个答案:

答案 0 :(得分:0)

对不起,我对递归模型不太熟悉。

但是我认为问题在于输入大小。

进入自定义图层的大小(?,8、32) 但是tf.nn.static_rnn需要类似

的列表

enter image description here

所以您需要更改输入大小,然后问题就会解决。

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Flatten, Dense

class MyLayer(tf.keras.layers.Layer):

    def __init__(self, **kwargs):
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        self.cell = tf.nn.rnn_cell.BasicRNNCell(32)
        super(MyLayer, self).build(input_shape)

    def call(self, x):
        "Some other tf function will be put at here"
        rnn_inputs = tf.unstack(x, axis=1)
        outputs, state = tf.nn.static_rnn(self.cell, rnn_inputs, dtype=tf.float32)
        for i in range(len(outputs)):
            outputs[i] = tf.expand_dims(outputs[i], axis=1)
        outputs = tf.concat(outputs, axis=1)
        return outputs

    def compute_output_shape(self, input_shape):
        return input_shape

def get_model(timesteps, data_dim):
    model = Sequential()
    model.add(LSTM(32, return_sequences=True, input_shape=(timesteps, data_dim)))
    model.add(LSTM(32, return_sequences=True))
    model.add(MyLayer())  # this is my layer

    model.add(Flatten())
    model.add(Dense(10, activation='softmax'))
    return model

def run_demo():
    data_dim = 16
    timesteps = 8
    num_classes = 10

    model = get_model(timesteps, data_dim)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    """Generate the traning and validation data"""
    x_train = np.random.random((1000, timesteps, data_dim))
    y_train = np.random.random((1000, num_classes))
    x_val = np.random.random((100, timesteps, data_dim))
    y_val = np.random.random((100, num_classes))

    model.fit(x_train, y_train, batch_size=64, epochs=5, validation_data=(x_val, y_val))

if __name__ == "__main__":
    run_demo()