问题描述
我正在尝试从文件中任何匹配的行中清除import tensorflow as tf
import tensorflow_hub as hub
import keras
class Output(keras.layers.Layer):
def __init__(self,name=None):
super(Output,self).__init__(name=name)
def call(self,inputs):
return tf.argmax(tf.tensordot(inputs[1],inputs[0],axes=(-1,-1)))
module_url = "https://tfhub.dev/google/universal-sentence-encoder-qa/3"
a = tf.keras.Input((),dtype='string')
c = tf.keras.Input((),dtype='string')
q = tf.keras.Input((),dtype='string')
response_e = hub.KerasLayer(module_url,input_shape=[],signature='response_encoder',output_key='outputs')({'input': a,'context': c})
question_e = hub.KerasLayer(module_url,signature='question_encoder',output_key='outputs')(q)
outputs = Output()([response_e,question_e])
model = tf.keras.Model(inputs=[a,c,q],outputs=outputs)
model.predict([tf.constant(['hello','howdy']),tf.constant(['good morning',"How's it going"]),tf.constant(['What is an example of a greeting?','How can I say hello?'])])
和行尾之间的所有内容。
我正在这样做:
UnimplementedError: Cast string to float is not supported
[[node functional_7/Cast_1 (defined at <ipython-input-12-e1ab62208da7>:14) ]][Op:__inference_predict_function_936411]
Function call stack:
predict_function
它几乎完美地工作。除了带有这些特殊字符的讨厌行之外,其余均保持不变。
//
我不知道这些字符是什么,我也不在乎。
为什么sed -i -e 's://.*$::g' file
与这些字符不匹配?
解决方法
文件(在这种情况下为iso-8859-1)与操作系统(在这种情况下为UTF-8)之间的编码不一致很容易导致麻烦。
将输入文件转换为系统的默认值UTF8。有几个实用程序可以做到这一点。一个是iconv。试试:
iconv -f iso-8859-1 -t utf8 file >newfile
,然后使用newfile
。
在旁边
另一个细微的问题是行尾。如果源文件是iso-8859-1
,是否曾经在Windows计算机上进行过编辑?如果是这样,它可能具有DOS / Windows行尾,当与Unix工具一起使用时,它们会引起各种非显而易见的问题。使用dos2unix
或类似的实用程序来转换行尾。