全局变量初始化时出现缺失占位符值崩溃?
Missing placeholder value crash occurs during global variable initialization?
在张量流 1.4 上。我收到 you need a value for placeholder tensor...
错误。事情是我 am 像
一样喂这个张量
feats = np.reshape(feats, (-1, var1, feat_dim, 1))
_, outlogits = sess.run([train_step, logits], feed_dict={inp_layer: feats,
targs: targets,
eta: 1e-4})
(通常我想在图表内部重塑形状,但出于调试目的,我已将其取出)
占位符:
inp_layer = tf.placeholder(tf.float32, shape=[None, var1, feat_dim, 1])
错误说:You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,66,200,1]
当我 运行 sess.run(tf.global_variables_initializer())
时出现此错误,所以它甚至还没有到达应该开始考虑占位符的部分,但它却在抱怨它们?!
我认为这可能与我的图层大小之一取决于占位符这一事实有关。(虽然我有 validate_shape=False
作为权重)。将添加更多代码。
编辑:失败的示例代码,指出我认为问题出在哪里(记住代码在全局变量 init 上失败):
!edit2:是的,问题在于那一行。那么问题就变成了我怎样才能有一个图表,其中一个权重(以及输出)的维度是动态的。
train_feats = '..'
train_json = '..'
feat_dim = 200
var1 = 20
batch_size = 64
inp_layer = tf.placeholder(tf.float32, shape=[None, var1, feat_dim, 1])
targs = tf.placeholder(tf.int64, shape=[None])
eta = tf.placeholder(tf.float32)
chunk_size = 3
w1 = init_weight([chunk_size, feat_dim, 1, 32])
b1 = tf.zeros([32])
a1 = conv_layer(inp_layer, w1, b1, stride=3, padding='VALID')
chunk_size = tf.shape(a1)[1] <==== # ! IS THE PROBLEM !
w5 = init_weight([chunk_size, 1, 32, 12])
b5 = tf.zeros([12])
a5 = conv_layer(a1, w5, b5, stride=1, padding='VALID', act=False)
logits_ = tf.reshape(a5, [-1, 12])
softmax = tf.nn.softmax(logits_)
cross_ent = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=targs,
logits=logits_))
train_step = tf.train.AdamOptimizer(eta).minimize(cross_ent)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for feats, targets in batch_gen(train_feats, train_json, var1, feat_dim):
feats = np.reshape(feats, (var1, var1, feat_dim, 1))
sess.run(train_step, feed_dict={inp_layer: bla,
targs: targets,
eta: 1e-4})
def init_weight(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.01), validate_shape=False)
def conv_layer(x, w, b, stride, padding, act=True):
# striding over the features
if act:
return tf.nn.elu(tf.nn.conv2d(x, w, [1, stride, 1, 1], padding) + b)
else:
return tf.nn.conv2d(x, w, [1, stride, 1, 1], padding) + b
行
chunk_size = tf.shape(a1)[1]
tf.shape
提取 a1
的运行时形状,而不是图形定义时已知的静态形状。由于 a1
是 inp_layer
和 w1
卷积的结果,因此当您引用 a1
时,您还需要解析 inp_layer
。因为 inp_layer
是一个占位符,所以您的错误如下。
由于您对图定义时已知的 a1
的第二个维度感兴趣,您可以只使用:
chunk_size = a1.shape[1].value
提取正确的维度值。
在张量流 1.4 上。我收到 you need a value for placeholder tensor...
错误。事情是我 am 像
feats = np.reshape(feats, (-1, var1, feat_dim, 1))
_, outlogits = sess.run([train_step, logits], feed_dict={inp_layer: feats,
targs: targets,
eta: 1e-4})
(通常我想在图表内部重塑形状,但出于调试目的,我已将其取出)
占位符:
inp_layer = tf.placeholder(tf.float32, shape=[None, var1, feat_dim, 1])
错误说:You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,66,200,1]
当我 运行 sess.run(tf.global_variables_initializer())
时出现此错误,所以它甚至还没有到达应该开始考虑占位符的部分,但它却在抱怨它们?!
我认为这可能与我的图层大小之一取决于占位符这一事实有关。(虽然我有 validate_shape=False
作为权重)。将添加更多代码。
编辑:失败的示例代码,指出我认为问题出在哪里(记住代码在全局变量 init 上失败):
!edit2:是的,问题在于那一行。那么问题就变成了我怎样才能有一个图表,其中一个权重(以及输出)的维度是动态的。
train_feats = '..'
train_json = '..'
feat_dim = 200
var1 = 20
batch_size = 64
inp_layer = tf.placeholder(tf.float32, shape=[None, var1, feat_dim, 1])
targs = tf.placeholder(tf.int64, shape=[None])
eta = tf.placeholder(tf.float32)
chunk_size = 3
w1 = init_weight([chunk_size, feat_dim, 1, 32])
b1 = tf.zeros([32])
a1 = conv_layer(inp_layer, w1, b1, stride=3, padding='VALID')
chunk_size = tf.shape(a1)[1] <==== # ! IS THE PROBLEM !
w5 = init_weight([chunk_size, 1, 32, 12])
b5 = tf.zeros([12])
a5 = conv_layer(a1, w5, b5, stride=1, padding='VALID', act=False)
logits_ = tf.reshape(a5, [-1, 12])
softmax = tf.nn.softmax(logits_)
cross_ent = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=targs,
logits=logits_))
train_step = tf.train.AdamOptimizer(eta).minimize(cross_ent)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for feats, targets in batch_gen(train_feats, train_json, var1, feat_dim):
feats = np.reshape(feats, (var1, var1, feat_dim, 1))
sess.run(train_step, feed_dict={inp_layer: bla,
targs: targets,
eta: 1e-4})
def init_weight(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.01), validate_shape=False)
def conv_layer(x, w, b, stride, padding, act=True):
# striding over the features
if act:
return tf.nn.elu(tf.nn.conv2d(x, w, [1, stride, 1, 1], padding) + b)
else:
return tf.nn.conv2d(x, w, [1, stride, 1, 1], padding) + b
行
chunk_size = tf.shape(a1)[1]
tf.shape
提取 a1
的运行时形状,而不是图形定义时已知的静态形状。由于 a1
是 inp_layer
和 w1
卷积的结果,因此当您引用 a1
时,您还需要解析 inp_layer
。因为 inp_layer
是一个占位符,所以您的错误如下。
由于您对图定义时已知的 a1
的第二个维度感兴趣,您可以只使用:
chunk_size = a1.shape[1].value
提取正确的维度值。