簡體   English   中英

如何在Keras模型Lambda層中預處理字符串?

[英]How to preprocess strings in Keras models Lambda layer?

我有一個問題,傳遞給Lambda層的值(在編譯時)是由keras生成的占位符(沒有值)。 編譯模型時,.eval()方法拋出錯誤:

您必須使用dtype字符串和形狀[?,1]為占位符張量'input_1'提供值

def text_preprocess(x):
  strings = tf.keras.backend.eval(x)
  vectors = []
  for string in strings:
    vector = string_to_one_hot(string.decode('utf-8'))
    vectors.append(vector)
  vectorTensor = tf.constant(np.array(vectors),dtype=tf.float32)
  return vectorTensor

input_text = Input(shape=(1,), dtype=tf.string)
embedding = Lambda(text_preprocess)(input_text)
dense = Dense(256, activation='relu')(embedding)
outputs = Dense(2, activation='softmax')(dense)

model = Model(inputs=[input_text], outputs=outputs)
model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])
model.summary()
model.save('test.h5')

如果我靜態地將字符串數組傳遞到輸入層,我可以編譯模型,但如果我想將模型轉換為tflite,我會得到相同的錯誤。

#I replaced this line:
input_text = Input(shape=(1,), dtype=tf.string)

#by this lines:
test = tf.constant(["Hello","World"])
input_text = Input(shape=(1,), dtype=tf.string, tensor=test)

#but calling this ...
converter = TFLiteConverter.from_keras_model_file('string_test.h5')
tfmodel = converter.convert()

#... still leads to this error:

InvalidArgumentError:您必須使用dtype字符串和形狀[2] [[{{node input_3}}]]為占位符張量'input_3'提供值

好吧,我終於解決了這個問題:

def text_preprocess(x):
  b = tf.strings.unicode_decode(x,'UTF-8')
  b = b.to_tensor(default_value=0)
  #do things with decoded string
  one_hot = K.one_hot(b,one_hot_size)
  return one_hot

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM