So I’m trying to expand the Roberta Pretrained Model and I was doing a basic model for testing but I’m getting this error from TensorFlow: ValueError: Output tensors of a Functional model must be the output of a TensorFlow Layer.
which is from the Model api of Keras but I don’t exactly know what’s causing it.
Code:
JavaScript
x
42
42
1
LEN_SEQ = 64
2
BATCH_SIZE = 16
3
TEST_TRAIN_SPLIT = 0.9
4
TRANSFORMER = 'roberta-base'
5
6
df = pd.read_csv('train-processed.csv')
7
df = df.head(100)
8
samples_count = len(df)
9
10
# Create labels
11
target = df['first_Sentiment'].values.astype(int)
12
labels = np.zeros((samples_count, target.max() + 1))
13
labels[np.arange(samples_count), target] = 1
14
15
tokenizer = AutoTokenizer.from_pretrained(TRANSFORMER)
16
tokens = tokenizer(
17
df['first_Phrase'].tolist(),
18
max_length=LEN_SEQ,
19
truncation=True,
20
padding='max_length',
21
add_special_tokens=True,
22
return_tensors='tf'
23
)
24
25
base_model = TFAutoModel.from_pretrained(TRANSFORMER)
26
27
embedding = base_model.roberta(input_ids=tokens['input_ids'], attention_mask=tokens['attention_mask'])
28
embedding.trainable = False
29
30
# Define inputs
31
input_ids = Input(shape=(LEN_SEQ,), name='input_ids', dtype='int32')
32
input_mask = Input(shape=(LEN_SEQ,), name='input_mask', dtype='int32')
33
34
# Define hidden layers
35
layer = Dense(LEN_SEQ * 2, activation='relu')(embedding[1])
36
layer = Dense(LEN_SEQ, activation='relu')(layer)
37
38
# Define output
39
output = Dense(target.max() + 1, activation='softmax', name='output')(layer)
40
41
model = Model(inputs=[input_ids, input_mask], outputs=[output])
42
Full error traceback:
JavaScript
1
146
146
1
---------------------------------------------------------------------------
2
ValueError Traceback (most recent call last)
3
<ipython-input-80-9a6ccb1b4ca8> in <module>
4
10 output = Dense(target.max() + 1, activation='softmax', name='output')(layer)
5
11
6
---> 12 model = Model(inputs=[input_ids, input_mask], outputs=[output])
7
13
8
14 model.compile(
9
10
/usr/local/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
11
528 self._self_setattr_tracking = False # pylint: disable=protected-access
12
529 try:
13
--> 530 result = method(self, *args, **kwargs)
14
531 finally:
15
532 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
16
17
/usr/local/lib/python3.8/site-packages/keras/engine/functional.py in __init__(self, inputs, outputs, name, trainable, **kwargs)
18
107 generic_utils.validate_kwargs(kwargs, {})
19
108 super(Functional, self).__init__(name=name, trainable=trainable)
20
--> 109 self._init_graph_network(inputs, outputs)
21
110
22
111 @tf.__internal__.tracking.no_automatic_dependency_tracking
23
24
/usr/local/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
25
528 self._self_setattr_tracking = False # pylint: disable=protected-access
26
529 try:
27
--> 530 result = method(self, *args, **kwargs)
28
531 finally:
29
532 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
30
31
/usr/local/lib/python3.8/site-packages/keras/engine/functional.py in _init_graph_network(self, inputs, outputs)
32
144 base_layer_utils.create_keras_history(self._nested_outputs)
33
145
34
--> 146 self._validate_graph_inputs_and_outputs()
35
147
36
148 # A Network does not create weights of its own, thus it is already
37
38
/usr/local/lib/python3.8/site-packages/keras/engine/functional.py in _validate_graph_inputs_and_outputs(self)
39
719 if not hasattr(x, '_keras_history'):
40
720 cls_name = self.__class__.__name__
41
--> 721 raise ValueError('Output tensors of a ' + cls_name + ' model must be '
42
722 'the output of a TensorFlow `Layer` '
43
723 '(thus holding past layer metadata). Found: ' + str(x))
44
45
ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: tf.Tensor(
46
[[0.18333092 0.18954797 0.22477032 0.21039596 0.1919548 ]
47
[0.18219706 0.1903447 0.2256843 0.20587942 0.19589448]
48
[0.18239683 0.1907878 0.22491893 0.20824413 0.19365236]
49
[0.18193942 0.1898969 0.2259874 0.20646562 0.1957107 ]
50
[0.18132213 0.1893883 0.22565623 0.21005587 0.1935775 ]
51
[0.18237704 0.18789911 0.22692119 0.20759228 0.1952104 ]
52
[0.18217668 0.18732095 0.22601548 0.21063867 0.19384828]
53
[0.1817196 0.18970788 0.22175607 0.21405536 0.19276106]
54
[0.18154216 0.18738106 0.22770867 0.2091297 0.19423842]
55
[0.1839993 0.19110405 0.2193769 0.2124882 0.19303149]
56
[0.18009834 0.19029345 0.22552258 0.21138497 0.19270067]
57
[0.18262982 0.18932794 0.22548872 0.20995376 0.19259974]
58
[0.18132062 0.1894746 0.22257458 0.21089785 0.19573237]
59
[0.18127124 0.18927224 0.2275273 0.2061446 0.19578464]
60
[0.18001163 0.1883382 0.22915907 0.20934238 0.19314879]
61
[0.18409619 0.19204247 0.22269006 0.20967877 0.19149245]
62
[0.18143429 0.18780865 0.22895294 0.21044146 0.1913626 ]
63
[0.18210162 0.18980804 0.22135185 0.21205473 0.1946838 ]
64
[0.18077913 0.18933856 0.22730026 0.2079047 0.19467732]
65
[0.18248595 0.19133545 0.2252994 0.20402898 0.1968502 ]
66
[0.18053354 0.18830904 0.22379933 0.21369977 0.19365832]
67
[0.18100418 0.1889128 0.22656825 0.21134934 0.19216539]
68
[0.18219638 0.18901002 0.22543809 0.20894748 0.194408 ]
69
[0.17991781 0.18693839 0.23250549 0.21227528 0.18836297]
70
[0.18322821 0.1881207 0.22497904 0.20976694 0.19390512]
71
[0.17972894 0.18888594 0.2251662 0.21268585 0.19353302]
72
[0.1822505 0.18769115 0.22729188 0.21127912 0.19148737]
73
[0.18432644 0.18830952 0.22477935 0.20987424 0.19271052]
74
[0.1801894 0.18920776 0.22684936 0.20734173 0.19641179]
75
[0.181594 0.1880084 0.22798598 0.20937674 0.19303486]
76
[0.18252885 0.19045824 0.22497422 0.207161 0.19487773]
77
[0.18196142 0.18878765 0.22479571 0.2105628 0.19389246]
78
[0.18600896 0.18686578 0.2283819 0.21188499 0.18685843]
79
[0.18056509 0.18865508 0.22694935 0.21080662 0.19302382]
80
[0.18446274 0.1887065 0.22405164 0.21271324 0.19006592]
81
[0.1812612 0.18995184 0.22384171 0.20790772 0.19703752]
82
[0.1861402 0.189157 0.2236694 0.21078445 0.19024895]
83
[0.18149142 0.18862149 0.2255336 0.20888737 0.19546609]
84
[0.18088317 0.1882689 0.22780944 0.20749897 0.19553955]
85
[0.1824722 0.18926203 0.22691077 0.2071967 0.1941583 ]
86
[0.18111941 0.18773855 0.22366299 0.21535842 0.19212064]
87
[0.18248987 0.18920848 0.22602491 0.20733926 0.19493747]
88
[0.18306294 0.19167435 0.22505572 0.21000686 0.19020009]
89
[0.18466519 0.1885763 0.22352514 0.21257839 0.19065501]
90
[0.18297954 0.18976018 0.2262897 0.20864752 0.19232307]
91
[0.18216778 0.18953851 0.22490299 0.21057723 0.1928135 ]
92
[0.18181367 0.19077264 0.2232015 0.21115994 0.1930523 ]
93
[0.18345618 0.18753015 0.22660162 0.20830849 0.1941036 ]
94
[0.18212378 0.18797131 0.2247642 0.21066691 0.19447377]
95
[0.18199605 0.19106121 0.22245005 0.21217921 0.19231346]
96
[0.18243583 0.18764758 0.22628336 0.21369886 0.18993443]
97
[0.18162242 0.18957089 0.22591078 0.20930369 0.19359224]
98
[0.18090473 0.18757755 0.22858356 0.20813066 0.19480348]
99
[0.17951688 0.18841572 0.22520997 0.21235934 0.19449812]
100
[0.1850496 0.18895829 0.22575855 0.20854111 0.1916925 ]
101
[0.18254244 0.18938984 0.22754729 0.20879866 0.19172177]
102
[0.1816532 0.18972425 0.22676478 0.20679341 0.19506434]
103
[0.18303266 0.19159187 0.22373216 0.20538329 0.19625996]
104
[0.18126963 0.18750906 0.2258774 0.21198079 0.1933631 ]
105
[0.18387978 0.18828613 0.22228165 0.21189795 0.19365448]
106
[0.1834729 0.18976368 0.22469373 0.20830937 0.19376035]
107
[0.18359789 0.18833868 0.22379532 0.21078889 0.19347927]
108
[0.18039297 0.18886234 0.22411437 0.2105467 0.19608359]
109
[0.17980678 0.18979622 0.2266618 0.20471531 0.19901991]
110
[0.18554561 0.19003332 0.22477089 0.21021138 0.18943883]
111
[0.18349187 0.18941568 0.22224301 0.21004184 0.19480757]
112
[0.18351436 0.19169463 0.22155108 0.21009424 0.19314568]
113
[0.18123321 0.18985166 0.22660086 0.21186577 0.19044854]
114
[0.18183744 0.192495 0.22091088 0.21275932 0.1919973 ]
115
[0.18028514 0.18943599 0.22416686 0.21241388 0.19369814]
116
[0.18061554 0.18873625 0.22677769 0.21073307 0.19313747]
117
[0.18186866 0.18851075 0.22588421 0.21183755 0.19189876]
118
[0.18126652 0.18949142 0.22501452 0.20897155 0.19525598]
119
[0.1835434 0.19079022 0.22333461 0.21146008 0.19087164]
120
[0.18269798 0.19171126 0.22150221 0.21224435 0.19184415]
121
[0.17996274 0.19000672 0.22470033 0.2105299 0.19480029]
122
[0.18345153 0.19032337 0.2239142 0.21167503 0.19063583]
123
[0.18224017 0.19025423 0.22567508 0.2087501 0.19308044]
124
[0.18233515 0.18966553 0.22833474 0.20635706 0.1933075 ]
125
[0.18210347 0.18650064 0.22770585 0.21101129 0.19267873]
126
[0.18199693 0.19086935 0.22255068 0.20988034 0.19470267]
127
[0.18119748 0.18983872 0.22518982 0.20845842 0.19531558]
128
[0.18367417 0.19071157 0.22310348 0.21277103 0.18973975]
129
[0.17965038 0.18936628 0.22479466 0.21279414 0.19339451]
130
[0.18141513 0.18989322 0.22380653 0.21031635 0.19456872]
131
[0.18295668 0.19067182 0.22385122 0.20624346 0.1962768 ]
132
[0.17981796 0.18981294 0.22544417 0.21043345 0.19449154]
133
[0.18068986 0.1897383 0.22433658 0.21027999 0.1949553 ]
134
[0.18146665 0.18844193 0.22996067 0.20703284 0.19309792]
135
[0.18278767 0.18972701 0.22451803 0.20893572 0.19403161]
136
[0.18077034 0.1892612 0.2236769 0.21081012 0.19548143]
137
[0.18254872 0.19220418 0.22300169 0.20895892 0.19328652]
138
[0.18032935 0.19029863 0.22319157 0.21000609 0.19617435]
139
[0.18328631 0.18907256 0.22911799 0.20782094 0.19070214]
140
[0.17863902 0.18771355 0.23066713 0.21065918 0.19232109]
141
[0.18178153 0.19022569 0.22538401 0.20857622 0.1940325 ]
142
[0.18072292 0.18907587 0.22616044 0.21096109 0.19307965]
143
[0.18215105 0.18966101 0.22436853 0.21200544 0.191814 ]
144
[0.18104836 0.18830387 0.22495148 0.21120267 0.19449359]
145
[0.18192047 0.18981694 0.22512193 0.2107065 0.19243418]], shape=(100, 5), dtype=float32)
146
Data example:
Any help appreciated. I’m new to transformers so please feel free to point any extra considerations.
Advertisement
Answer
You need to pass a list
of [input_ids , input_mask]
to base_model
.
JavaScript
1
33
33
1
# !pip install transformers
2
from transformers import TFAutoModel
3
import tensorflow as tf
4
5
6
LEN_SEQ = 64
7
8
# Define inputs
9
input_ids = tf.keras.layers.Input(shape=(LEN_SEQ,), name='input_ids', dtype='int32')
10
input_mask = tf.keras.layers.Input(shape=(LEN_SEQ,), name='input_mask', dtype='int32')
11
12
base_model = TFAutoModel.from_pretrained('roberta-base')
13
for layer in base_model.layers:
14
layer.trainable = False
15
# Check summary of tf_roberta_model
16
base_model.summary()
17
18
embedding = base_model([input_ids, input_mask])[1]
19
# Or
20
# embedding = base_model([input_ids, input_mask]).pooler_output
21
22
23
# Define hidden layers
24
layer = tf.keras.layers.Dense(LEN_SEQ * 2, activation='relu')(embedding)
25
layer = tf.keras.layers.Dense(LEN_SEQ, activation='relu')(layer)
26
27
# Define output
28
output = tf.keras.layers.Dense(2, activation='softmax', name='output')(layer)
29
30
31
model = tf.keras.models.Model(inputs=[input_ids, input_mask], outputs=[output])
32
model.summary()
33
Output:
JavaScript
1
47
47
1
Model: "tf_roberta_model_2"
2
_________________________________________________________________
3
Layer (type) Output Shape Param #
4
=================================================================
5
roberta (TFRobertaMainLayer multiple 124645632
6
)
7
8
=================================================================
9
Total params: 124,645,632
10
Trainable params: 0
11
Non-trainable params: 124,645,632
12
_________________________________________________________________
13
14
15
Model: "model"
16
__________________________________________________________________________________________________
17
Layer (type) Output Shape Param # Connected to
18
==================================================================================================
19
input_ids (InputLayer) [(None, 64)] 0 []
20
21
input_mask (InputLayer) [(None, 64)] 0 []
22
23
tf_roberta_model_2 (TFRobertaM TFBaseModelOutputWi 124645632 ['input_ids[0][0]',
24
odel) thPoolingAndCrossAt 'input_mask[0][0]']
25
tentions(last_hidde
26
n_state=(None, 64,
27
768),
28
pooler_output=(Non
29
e, 768),
30
past_key_values=No
31
ne, hidden_states=N
32
one, attentions=Non
33
e, cross_attentions
34
=None)
35
36
dense (Dense) (None, 128) 98432 ['tf_roberta_model_2[0][1]']
37
38
dense_1 (Dense) (None, 64) 8256 ['dense[0][0]']
39
40
output (Dense) (None, 2) 130 ['dense_1[0][0]']
41
42
==================================================================================================
43
Total params: 124,752,450
44
Trainable params: 106,818
45
Non-trainable params: 124,645,632
46
__________________________________________________________________________________________________
47