首页 > 解决方案 > 提供形状为 (?, n) 的占位符时出现问题

问题描述

我有一个问题要喂我的占位符。我有以下错误消息:

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()

非常感谢!!

标签: pythontensorflowplaceholder

解决方案


推荐阅读