我試圖從 TensorFlow 中心獲得一個簡單的 ELMO 模型,但結果證明這是一個挑戰。
當我運行我的代碼時,我收到錯誤:“急切執行函式的輸入不能是 Keras 符號張量,但發現 [<tf.Tensor 'input_69:0' shape=(None, 10) dtype=string>] ”
我想我弄亂了 sequence_length 引數或輸入。誰能幫幫我嗎?
import tensorflow as tf
import tensorflow_hub as hub
import re
from tensorflow import keras
import tensorflow.keras
from tensorflow.keras.layers import Input, Dense,Flatten
import numpy as np
import keras.callbacks
import io
from sklearn.model_selection import train_test_split
i = 0
max_cells = 51 #countLines()
x_data = np.zeros((max_cells, 10, 1), dtype='object')
y_data = np.zeros((max_cells, 3), dtype='float32')
seqs = np.zeros((max_cells), dtype='int32')
with io.open('./data/names-sample.txt', encoding='utf-8') as f:
content = f.readlines()
for line in content:
line = re.sub("[\n]", " ", line)
tokens = line.split()
for t in range(0, min(10,len(tokens))):
tkn = tokens[t]
x_data[i,t] = tkn
seqs[i] = len(tokens)
y_data[i,0] = 1
i = i 1
def build_model():
tokens = Input(shape=[10,], dtype=tf.string)
seq_lens = Input(shape=[], dtype=tf.int32)
elmo = hub.KerasLayer(
"https://tfhub.dev/google/elmo/3",
trainable=False,
output_key="elmo",
signature="tokens",
)
out = elmo({"tokens": tokens, "sequence_len": seqs})
model = keras.Model(inputs=[tokens, seq_lens], outputs=out)
model.compile("adam", loss="sparse_categorical_crossentropy")
model.summary()
return model
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.70, shuffle=True)
model = build_model()
model.fit(x_train, y_train,validation_data=(x_test, y_test),epochs=1,batch_size=32)
完整錯誤:
型別錯誤:函式構建代碼之外的操作正在傳遞“圖形”張量。通過在函式構建代碼中包含 tf.init_scope ,可能會使 Graph 張量從函式構建背景關系中泄漏。例如,以下函式將失敗:@tf.function def has_init_scope(): my_constant = tf.constant(1.) with tf.init_scope(): added = my_constant * 2 圖張量名稱:input_69:0
在處理上述例外的程序中,又發生了一個例外:
回溯(最近一次呼叫最后一次):
檔案“C:\temp\Simon\TempElmoNames.py”,第 66 行,模型 = build_model()
檔案“C:\temp\Simon\TempElmoNames.py”,第 56 行,在 build_model out = elmo({"tokens": tokens, "sequence_len": seqs})
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 891, in call outputs = self.call(cast_inputs, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_hub\keras_layer.py", line 229, in call result = f()
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1081, in call return self._call_impl(args, kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1121, in _call_impl return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1224, in _call_flat ctx, args, cancellation_manager=cancellation_manager)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 511, in call ctx=ctx)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\eager\execute.py", line 75, in quick_execute "tensors, but found {}".format(keras_symbolic_tensors))
_SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'input_69:0' shape=(None, 10) dtype=string>]
Here are the versions I'm working with: Keras: 2.3.1 TF: 2.0.0 TH-hub: 0.12.0
更新 1: 我升級了 Keras (2.6.0) TF (2.6.0) & TF Hub(0.12.0) 并更改了關于 seqs 和 seq_lens 如何傳遞的 build_model 方法。
def build_model():
tokens = Input(shape=[10,], dtype=tf.string)
seq_lens = Input(shape=[], dtype=tf.int32)
elmo = hub.KerasLayer(
"https://tfhub.dev/google/elmo/3",
trainable=False,
output_key="elmo",
signature="tokens",
)
out = elmo({"tokens": tokens, "sequence_len": seq_lens})
model = keras.Model(inputs=[tokens, seqs], outputs=out)
model.compile("adam", loss="sparse_categorical_crossentropy")
model.summary()
return model
現在我收到錯誤:
ValueError:函式的輸入張量必須來自
tf.keras.Input. 收到: [3 3 2 2 3 3 3 5 3 3 3 2 7 2 2 2 3 2 2 3 3 3 3 3 3 2 3 2 3 2 3 3 2 3 3 2 3 2 2 2 3 2 2 3 3 5 3 3 3 0](缺少前一層元資料)。
uj5u.com熱心網友回復:
我不認為這是一個錯誤,而是 TF 給了我們選擇每種方法的自由。雖然我們可以將層子類與 keras 功能 api 混合匹配,但我想我們不能讓模型子類與 keras 的模型 api 一起作業。在我看來,這就是急切執行和 keras 圖模式之間的區別發生沖突的地方,從而導致了這種“SymbolicException”。
事先讓 TF 知道它應該執行什么模式可以解決它。
uj5u.com熱心網友回復:
好的終于讓它作業了。我做的第一個升級:
Keras: 2.2.4
TF: 1.15.0
TF: 0.12.0
接下來更改我的代碼以使用正確版本的 ELMO 模型:
import tensorflow_hub as hub
import tensorflow as tf
elmo = hub.Module("https://tfhub.dev/google/elmo/3", trainable=False)
from tensorflow.keras.layers import Input, Lambda, Bidirectional, Dense, Dropout, Flatten, LSTM
from tensorflow.keras.models import Model
def ELMoEmbedding(input_text):
return elmo(tf.reshape(tf.cast(input_text, tf.string), [-1]), signature="default", as_dict=True)["elmo"]
def build_model():
input_layer = Input(shape=(1,), dtype="string", name="Input_layer")
embedding_layer = Lambda(ELMoEmbedding, output_shape=(1024, ), name="Elmo_Embedding")(input_layer)
BiLSTM = Bidirectional(LSTM(128, return_sequences= False, recurrent_dropout=0.2, dropout=0.2), name="BiLSTM")(embedding_layer)
Dense_layer_1 = Dense(64, activation='relu')(BiLSTM)
Dropout_layer_1 = Dropout(0.5)(Dense_layer_1)
Dense_layer_2 = Dense(32, activation='relu')(Dropout_layer_1)
Dropout_layer_2 = Dropout(0.5)(Dense_layer_2)
output_layer = Dense(3, activation='sigmoid')(Dropout_layer_2)
model = Model(inputs=[input_layer], outputs=output_layer, name="BiLSTM with ELMo Embeddings")
model.summary()
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
return model
elmo_BiDirectional_model = build_model()
import numpy as np
import io
import re
from tensorflow import keras
i = 0
max_cells = 300
x_data = np.zeros((max_cells, 1), dtype='object')
y_data = np.zeros((max_cells, 3), dtype='float32')
with tf.Session() as session:
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
model_elmo = elmo_BiDirectional_model.fit(x_data, y_data, epochs=100, batch_size=5)
train_prediction = elmo_BiDirectional_model.predict(x_data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338092.html
