计算梯度时出现以下错误。
ValueError:运算输入和计算的输入梯度之间的形状不兼容。转发操作:write_8 / conv1d_transpose。输入索引:2。原始输入形状:(100,1,10,10,100)。计算得出的输入渐变形状:(100,1,13,10)
def conv1d_layer(inp, filters, stride, output_shape=None, transpose=False):
if output_shape is not None:
output_shape = tf.constant(output_shape)
if transpose: return tf.contrib.nn.conv1d_transpose(inp,
filters, output_shape, stride, padding='SAME')
return tf.nn.conv1d(inp, filters, stride, 'SAME')
def forward(input, batch_size=100):
output = tf.reshape(output, [-1, 10, 10, 100])
init = tf.truncated_normal_initializer(stddev=1.)
filter = tf.get_variable('f1', [5, 1, 10], initializer=init)
output = conv1d_layer(output, filter, 2, [batch_size, 25, 25], True)
filter = tf.get_variable('f2', [5, 1, 25], initializer=init)
output = conv1d_layer(output, filter, 2, [batch_size, 50, 50], True)
filter = tf.get_variable('f3', [5, 1, 50], initializer=init)
out = conv1d_layer(output, filter, 2, [batch_size, 100, 100], True)
out = tf.reshape(out, [-1, 100 * 100])
vocab_size = 10000
w = tf.get_variable('w', [100*100, vocab_size], initializer=init)
b = tf.Variable(tf.zeros([vocab_size])) # biases
return tf.matmul(output, w) + b
在提供一些输入后,我通过调用conv1d_layer进行前向传递。然后,当我按如下方式计算梯度时,即发生了错误。
output = forward(input)
loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=output, labels=classes)
optimizer = tf.train.AdamOptimizer(lr, beta1=0.5)
grads = optimizer.compute_gradients(loss)
我怀疑我的过滤器是错误的(我希望最终形状为[100,100,vocab_size],其中vocab_size = 10000,如上所述。我们将不胜感激。