python - 提供形状为 (?, 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()
非常感谢!!
解决方案
推荐阅读
- python - 创建数据框时没有索引
- java - 我在放心的代码中收到“java.util.zip.ZipException:未知压缩方法”
- ansible - 使用 jmespath 检查嵌套数据中所有键的条件,纠正“倾斜牙签综合症”
- node.js - AWS EC2 上的节点服务器突然超时所有请求但继续运行。重新启动服务器暂时解决了问题,但它继续
- vue.js - How to make this.$refs.form method work by creating the component before?
- javascript - React:变量中的 jsx vs 函数 vs 单独的组件
- angular - 如果在没有 authGuard 的情况下使用 firebase 解决,Angular Universal 不会预渲染模块
- react-native - 元素类型无效:预期的字符串或类/函数但未定义
- revit-api - 有没有办法使用 Revit API 修改连接的墙壁,例如房子里的墙壁?
- reactjs - 在反应片段中使用语法形式时,编辑器文本颜色将被更改。如何解决这个问题呢