如何将顺序模型更改为自定义类模型

时间:2019-08-24 12:32:41

标签: tensorflow keras keras-layer

我正在从其旧版本中学习tensorflow 2.0。 我发现tensorflow模型已从Class-base更改为Sequential-base。 但是我想使用基于类的模型,因为它很容易阅读。

我想尝试翻译:https://www.tensorflow.org/beta/tutorials/keras/basic_text_classification_with_tfhub

embedding = 'https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1'
hub_layer = hub.KerasLayer(embedding,
                           input_shape=[],
                           dtype=tf.string,
                           trainable=True)
# hub_layer(train_example_batch[:3])


# model = tf.keras.Sequential()
# model.add(hub_layer)
# model.add(tf.keras.layers.Dense(16, activation='relu'))
# model.add(tf.keras.layers.Dense(1, activation='sigmoid'))


class MyModel(keras.Model):
    def __init__(self, embedding):
        super(MyModel, self).__init__()
        self.embedding = embedding
        self.d1 = keras.layers.Dense(16, activation='relu')
        self.d2 = keras.layers.Dense(1, activation='sigmoid')

    def call(self, x):
        print(x.shape)
        return reduce(lambda x, f: f(x), [x, self.embedding, self.d1, self.d2])


model = MyModel(hub_layer)

我收到以下错误消息。

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  input must be a vector, got shape: [512,1]
     [[{{node my_model_48/keras_layer_7/StatefulPartitionedCall/StatefulPartitionedCall/StatefulPartitionedCall/tokenize/StringSplit}}]]
  (1) Invalid argument:  input must be a vector, got shape: [512,1]
     [[{{node my_model_48/keras_layer_7/StatefulPartitionedCall/StatefulPartitionedCall/StatefulPartitionedCall/tokenize/StringSplit}}]]
     [[my_model_48/keras_layer_7/StatefulPartitionedCall/StatefulPartitionedCall/StatefulPartitionedCall/SparseFillEmptyRows/SparseFillEmptyRows/_24]]
0 successful operations.
0 derived errors ignored. [Op:__inference_keras_scratch_graph_303077]

Function call stack:
keras_scratch_graph -> keras_scratch_graph

为什么会出现此错误?另外,请回答我们是否需要扔掉基于类的模型?

1 个答案:

答案 0 :(得分:0)

这是正确的代码。

# model = tf.keras.Sequential()
# model.add(hub_layer)
# model.add(tf.keras.layers.Dense(16, activation='relu'))
# model.add(tf.keras.layers.Dense(1, activation='sigmoid'))


class MyModel(keras.Model):
    def __init__(self, embedding):
        super(MyModel, self).__init__()
        self.embedding = embedding
        self.d1 = keras.layers.Dense(16, activation='relu')
        self.d2 = keras.layers.Dense(1, activation='sigmoid')

    def call(self, x):
        # tf.sequeeze is needed! because x's dimention is [None, 1]. (1 was inserted without permission ...)
        return reduce(lambda x, f: f(x), [x, tf.squeeze, self.embedding, self.d1, self.d2])


model = MyModel(hub_layer)

# model.summary()
model.layers

这是因为恶意框架自己添加了额外的功能...

我不喜欢此属性,但我认为喜欢tensorflow的人渴望做到这一点...