问题
I'm learning tensorflow 2.0 from its older versions. I found tensorflow model is changed Sequential-base from Class-base. But I want to use Class-base model because it is easy to read for me.
I want to try translate : 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)
I got below error message.
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
Why I got this error? And also, please answer whether WE NEED THROWING AWAY Class-base Model?
回答1:
Here is the right code.
# 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
It is because the officious framework has added extra features on their own...
I don't like this attribute, but I think someone who like tensorflow eager to do it...
来源:https://stackoverflow.com/questions/57637985/how-to-change-sequential-model-to-custom-class-model