我在喂占位符时遇到问题。我收到以下错误消息:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'NSregex_9/Placeholder' with dtype float and shape [?,30]
我用形状为[1,30]的np.array填充了占位符:
with tf.Session() as session:
session.run(tf.global_variables_initializer())
regexp_vector = generate_tf_vector_from_list_regexp(NSinput, regexplist)
regexp_vector = np.expand_dims(regexp_vector, axis=0)
print("regexp_vector", regexp_vector, type(regexp_vector))
session.run(tf.tables_initializer(), feed_dict={NSregex: regexp_vector})
history = NSmodel.fit(input_train,
output_train,
validation_data=(input_test, output_test),
epochs=100,
batch_size=32)
generate_tf_vector_from_list_regexp(x,y)的输出是一个大小为30的numpy数组。因此,我将其扩展1个维度以适应我的pleceholder形状。
因此,我正在努力寻找正确填充占位符的解决方案。我模型的每一层都具有(?,n)的形状。问号(?)应该根据batch_size进行评估。。。我看不出哪里错了。
您可以在下面的代码中找到整个上下文:
def generate_tf_vector_from_list_regexp(s, listr):
'''
input is string tensor
listr is a string list of regexp
'''
v=[]
for regexp in listr:
if tf.reshape(tf.strings.regex_full_match(s,tf.convert_to_tensor(regexp, dtype=tf.string)), [])==True:
v.append(1.0)
else:
v.append(0.0)
return np.asarray(v)
regexEmbedding lamnda函数只是创建我的placeHolder的lambda函数:
def RegexEmbedding(x):
return = tf.placeholder(tf.float32, shape=[None, nb_classe])
和模型:
NSinput = layers.Input(shape=(1,), dtype=tf.string, name='NSinput')
NSuse = layers.Lambda(UniversalEmbedding, output_shape=(embed_size,), name="NSuse")(NSinput)
NSregex = layers.Lambda(RegexEmbedding, input_shape=(1,), output_shape=(nb_classe,), name="NSregex")(NSinput)
NSconcat = layers.concatenate([NSuse, NSregex], axis=1)
NSdenseConcat = layers.Dense(256, input_shape=(dims,), activation='relu', name="NSdenseConcat")(NSconcat)
NSoutput = layers.Dense(nb_classe, activation='softmax', name="NSoutput")(NSdenseConcat)
NSmodel = Model(inputs=NSinput, outputs=NSoutput)
NSmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
NSmodel.summary()
非常感谢!